Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a public folder in my document root. I would like to redirect all the files that are not present in document root, but are present in the public folder. (So if the same file is in two places, the file that is not in the public will be served first.)
How can I do this ?
The following doesn't work:

RewriteCond %{DOCUMENT_ROOT}/public/%{REQUEST_URI} -f¬
RewriteRule ^(.*)$ public/$1 [QSA, PT, L]
share|improve this question
My example is working. The problem was the AllowOverride None in the server main configuration file. – kissgyorgy Jan 22 '12 at 18:22
It doesn't exactly do the described behavior though. It first check the file and rewrite if exists. – kissgyorgy Jan 22 '12 at 18:28
up vote 3 down vote accepted
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1
share|improve this answer
perfect ! thanks – kissgyorgy Jan 22 '12 at 18:28
however I suggest to make an other RewriteCond too RewriteCond %{REQUEST_FILENAME} !-d for directories – kissgyorgy Jan 23 '12 at 9:28
Despite upvotes, it appears to me this will not actually work, firstly because there is no check that the file does exist in the /public folder. (All the second RewriteCond does is prevent double-rewriting of requests for direct access to the /public folder, something that's a good idea but that has little to do with the OPs stated problem.) Secondly, using REQUEST_URI assumes there's no remapping whatsoever from external URL to internal subdirectories and will not work if there is. And thirdly, as noted by the previous comment, requests may be for directories as well as files. – Chuck Kollars Sep 28 '15 at 23:24
@ChuckKollars there's no need to check whether it exists in /public because if it doesn't, you get your 404 whether or not the RewriteRule runs. You're right about the purpose of the second (now third) RewriteCond (it can get relevant quickly if you have cgi programs though). And yes, this relies on REQUEST_FILENAME to be "the full local filesystem path to the file or script matching the request" (see docs) which is not always the case so you might need to customize further. Thanks for the reminder about -d, added now. – tomclegg Sep 30 '15 at 4:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.