Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
933 views
in Technique[技术] by (71.8m points)

apache - Rewrite Problem - L(ast) not being respected?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...