重写问题 - L(AST)不被尊重?重写、不被、问题、AST

2023-09-02 00:20:01 作者:始于心动

所以我工作的一个CSS / JS COM pressing系统的网站,这已经基本下的htaccess

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}

现在assets.php没有收到哈希电话,而是index.php文件 - 如果我删除行重写规则^([A-ZA-Z0-9 _.-] +)$ index.php文件的URL = $ 1安培;%{QUERY_STRING} [L]

它工作正常,但我不知道为什么 - 不应该对资产[L]标志重写重写规则^ CSS /([0-9A-FA-F] {32}) $ assets.php?哈希= $ 1 [L] prevent执行任何进一步重写?我被什么这里发生混淆。

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.

您可以摆脱这个任何光线将大大AP preciated。

Any light you could shed on this would be much appreciated.

推荐答案

标记表示不执行任何更多的规则,在规则集,这奇怪的呢但并不意味着没有更多的办法由的mod_rewrite 执行。

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.

当您在每个目录范围内指定的mod_rewrite 指令,像的.htaccess 目录服务器或虚拟服务器配置的部分,改写来得晚在Apache处理阶段。在这里做它的魔力,的mod_rewrite 必须执行内部重定向每次您的URL重写一次。

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.

由于您的重写可能指向你到不同的目录,的mod_rewrite 自己指定的处理程序,这个重定向,以便它可以通过任何规则,它可能会发现在新位置你发送的请求。通常情况下,因为你只处理与根一个的.htaccess 文件,在规则的新的位置恰好是那些导致重写的第一位。

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:

/ CSS / A01EF 发请求 的mod_rewrite 启动规则集 ^ CSS /([0-9A-FA-F] {32})$ - > assets.php?哈希= A01EF 标志停止重写和部队内部重定向到 assets.php?哈希= A01EF 的mod_rewrite 启动规则集重新 ^([A-ZA-Z0-9 _.-] +)$ - > 的index.php URL = assets.php&放?;哈希= A01EF (你可以使用 QSA 在这里,顺便说一下) 标志停止重写和部队内部重定向到的index.php URL = assets.php和放大器;哈希= A01EF 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

这可能是因为这种循环将继续,但的mod_rewrite 认识到,你重定向到相同的页面,并在这之后将忽略重写。

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

...在的.htaccess 的mod_rewrite 规则集如此普遍,因为它们提供了一种简单的方法来确定如果该URL已被重写,以预期的实际资源。你可以使用它们,也可以排除当请求已被重写 assets.php 的index.php 重写

...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]