Score:1

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

ws flag

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.

Score:1
kz flag

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.)

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.