So I’m working on a CSS/JS compressing system for a site, that has basically the following htaccess
RewriteEngine On
...
RewriteRule ^css/images/(.*)$ images/site/$1?%{QUERY_STRING} [L]
RewriteRule ^css/([0-9a-fA-F]{32})$ assets.php?hash=$1 [L]
RewriteCond %{HTTP_HOST} ^www.site.com [NC]
RewriteRule ^(.*)$ http://site.com/$1 [L,R=301]
RewriteRule ^([a-zA-Z0-9_.-]+)$ index.php?url=$1&%{QUERY_STRING} [L]
php_flag register_globals off
php_flag magic_quotes_gpc off
php_flag register_long_arrays off
# 404 Handler
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1&%{QUERY_STRING}
Right now assets.php isn’t receiving the hash call, but rather index.php – if I remove the line RewriteRule ^([a-zA-Z0-9_.-]+)$ index.php?url=$1&%{QUERY_STRING} [L]
it works fine but I don’t know why – shouldn’t the [L] flag on the assets rewrite RewriteRule ^css/([0-9a-fA-F]{32})$ assets.php?hash=$1 [L] prevent any further rewrites from being executed? I’m confused by whats happening here.
Any light you could shed on this would be much appreciated.
The
Lflag says “do not execute any more rules in the ruleset,” which oddly enough does not imply that no more rules will be executed bymod_rewrite.When you specify
mod_rewritedirectives in a per-directory context, like with.htaccessor theDirectorysection of a server or virtual server configuration, the rewrite comes late in the Apache processing stage. To do its magic here,mod_rewritehas to perform an internal redirect every time that your URL is rewritten.Since your rewrite could point you to a different directory,
mod_rewriteassigns itself as the handler for this redirection so that it can go through whatever rules it may find at the new location you’ve sent the request to. Often, since you’re only dealing with a single.htaccessfile in your root, the rules in the “new” location happen to be the ones that caused the rewrite in the first place.So, in your case, the following happens:
/css/A01EFmod_rewritestarts the ruleset^css/([0-9a-fA-F]{32})$ -> assets.php?hash=A01EFLflag stops rewrite and forces internal redirect toassets.php?hash=A01EFmod_rewritestarts the ruleset over again^([a-zA-Z0-9_.-]+)$->index.php?url=assets.php&hash=A01EF(you could useQSAhere, by the way)Lflag stops rewrite and forces internal redirect toindex.php?url=assets.php&hash=A01EFIt’s likely that this loop would continue, but
mod_rewriterecognizes that you’re redirecting to the same page and ignores your rewrite after this point.This whole process happens to be why the two conditions…
…are so common in
.htaccessmod_rewriterulesets, given that they provide an easy way to determine if the URL has already been rewritten to the intended real resource. You could use them, or you can exclude theindex.phprewrite when the request has been rewritten toassets.php: