2

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]
kissgyorgy
  • 302
  • 1
  • 5
  • 16

1 Answers1

6
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1
tomclegg
  • 301
  • 1
  • 2
  • however I suggest to make an other RewriteCond too `RewriteCond %{REQUEST_FILENAME} !-d` for directories – kissgyorgy Jan 23 '12 at 09: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](http://httpd.apache.org/docs/current/mod/mod_rewrite.html)) 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 04:26