How to load requested file from sub-directory without rewriting base

Utilizing .htaccess rules, I'm attempting to load a file from within a sub-directory while keeping the frontend URL unchanged if both the domain and file path match criteria.

Example of URL:

example.com/specificfile.xml

The file itself is within a sub-directory that is not the public web root:

/var/www/feeds/specificfile.xml

While attempting to Google this, I'm getting a lot of results for RewriteBase but that's not what I'm after. I need to "rewrite base directory" for only a single file and retain the frontend URL.


Solution 1:

Assuming /var/www is the document root then in order to serve /feeds/specificfile.xml when requesting /specificfile.xml you would do the following using mod_rewrite in the root .htaccess file:

RewriteRule ^specificfile\.xml$ feeds/$0 [L]

For this to work, RewriteBase must either be not set at all (preferable) or set to RewriteBase /.

The $0 backreference simply saves repetition - it refers to the entire URL-path that is matched by the RewriteRule pattern, ie. specificfile.xml.

Note that if you have existing mod_rewrite directives in your .htaccess file then order matters. (eg. External/canonical redirects need to go first.)