Changing REQUEST_URI and REQUEST_FILENAME across rewrites

Topics: User Forum
Apr 4, 2011 at 12:33 PM

Hello Cheeso,

Hello IIRF users,


I'd like to know what's the reason behind the decision of not changing REQUEST_URI and REQUEST_FILENAME across rewrites.

AFAIK, but I may be wrong, apache's mod_rewrite changes at least the REQUEST_URI (see, for example,


The reason I'm asking is that I have a need to first perform a rewrite and then to call an axd only if the file from the previous rewritten url does not exist, something like:


RewriteRule ^/photos/(.*)/([a-z0-9]+) /cache/$2.jpg

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule /cache/([a-z0-9]+).jpg /pichandler.axd?picid=$1 [L]


What I'm trying to achieve is to be able to load a picture from either

http://localhost/my-funky-seo-description/az034, and



The problem I'm seeing now is that since REQUEST_URI/REQUEST_FILENAME variables are NOT modified across rewrites, the second rewrite fails.


Thank you for any pointers you might have.



Apr 4, 2011 at 6:41 PM

Right - I understand.

A way around the obstacle you described: you can test for file existence without referencing the REQUEST_FILENAME server variable.  For example,

RewriteCond %{APPL_PHYSICAL_PATH}/cache/$2.jpg  !-f
RewriteRule ^/photos/(.*)/([a-z0-9]+) /cache/$2.jpg

The above rule would NOT apply the rewrite, unless the file exists in the cache. This doesn't solve your problem completely - you'd still need a different rule to rewrite to the "other" option.