The L
flag says "do not execute any more rules in the ruleset," which oddly enough does not imply that no more rules will be executed by mod_rewrite
.
When you specify mod_rewrite
directives in a per-directory context, like with .htaccess
or the Directory
section of a server or virtual server configuration, the rewrite comes late in the Apache processing stage. To do its magic here, mod_rewrite
has to perform an internal redirect every time that your URL is rewritten.
Since your rewrite could point you to a different directory, mod_rewrite
assigns 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 .htaccess
file 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:
- Request made for
/css/A01EF
mod_rewrite
starts the ruleset
^css/([0-9a-fA-F]{32})$ -> assets.php?hash=A01EF
L
flag stops rewrite and forces internal redirect to assets.php?hash=A01EF
mod_rewrite
starts the ruleset over again
^([a-zA-Z0-9_.-]+)$
-> index.php?url=assets.php&hash=A01EF
(you could use QSA
here, by the way)
L
flag stops rewrite and forces internal redirect to index.php?url=assets.php&hash=A01EF
It's likely that this loop would continue, but mod_rewrite
recognizes 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...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
...are so common in .htaccess
mod_rewrite
rulesets, 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 the index.php
rewrite when the request has been rewritten to assets.php
:
RewriteCond %{REQUEST_URI} !^/assets.php
RewriteRule ^([a-zA-Z0-9_.-]+)$ index.php?url=$1&%{QUERY_STRING} [L]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…