12/18/2005
What’s up with REQUEST_FILENAME in mod_rewrite?
I was trying to setup mod_rewrite rules for a CakePHP app running under Apache.
According to the mod\_rewrite docs, REQUEST\_FILENAME is “The full local filesystem path to the file or
script matching the request.”
If that were true, this idiom, which I’ve seen in a number of places,
should work to prevent the RewriteRule underneath it from firing when a request is made for a file which exists in the filesystem:
RewriteCond %{REQUEST_FILENAME} !-f
But it doesn’t. Looking at the rewrite log reveals that REQUEST_FILENAME, on my system at least, is not the full filesystem path; it is actually relative to the document root. Which means that the condition is never true and the rewrite will always happen.
So in order to get it to work, I have to do this ugly hack:
RewriteCond /home/y/share/htdocs%{REQUEST_FILENAME} !-f
Why?
Del.icio.us
Digg
Reddit
Technorati
Possibly related posts
Comments
Leave a reply



Did you ever find the solution to this? I’m having the same problem with a rewrite thing I’m doing at the moment..
cheers,
Jonathan.
Looks like -f is context sensitive, I just ran into this problem, when used in .htaccess or inside a tag, it works correctly, outside it has the same behaviour as %{REQUEST_URI}, so:
DocumentRoot “C:Site”
ServerName http://www.mysite.com
Will behave badly, however:
DocumentRoot “C:Site”
ServerName http://www.mysite.com
Will work as it’s supposed to.
Thanks
–
Craig “FrostyCoolSlug” McLure
Disregard my previous comment, seems that some cached stuff was causing confusion on the page..
A more simple solution, when looking at your example, change:
RewriteCond %{REQUEST_FILENAME} !-f
To read:
RewriteCond %{DOCUMENTROOT}%{REQUESTFILENAME} !-f
Thus getting a Document Root prefix. It is still context specific, so if you use .htaccess you don’t need %{DOCUMENT_ROOT} but from what i’ve seen, it shouldn’t go in , my apologies for any confusion.
Your rule works fine, Frosty. However, if you are using Alias’ed resources and wish to do the “-f” test, you’re back in the same boat.
It seems Apache doesn’t know how to check to see if a file exists when it’s being referenced via an alias.