apache 即使使用 L 标志,mod_rewrite 也会循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2127131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mod_rewrite loops even with L flag
提问by viraptor
I've got a problem with rewriting a URL to a fastcgi dispatcher. If I leave only:
我在将 URL 重写为 fastcgi 调度程序时遇到问题。如果我只离开:
RewriteRule ^(.*)$ dispatch.fcgi/ [L,QSA]
I expected L(last rule) to cause only a single rewrite. Instead, it keeps prepending dispatch.fcgiuntil apache reports an error.
我预计L(最后一条规则)只会导致一次重写。相反,它一直在前面,dispatch.fcgi直到 apache 报告错误。
I know it can be fixed with:
我知道它可以通过以下方式修复:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi/ [L,QSA]
But what is the reason for multiple rewrites? Does Ldo something else than I think it does?
但是多次重写的原因是什么?是否L做别的事情比我想象的那样?
回答by Denes Papp
I know it's an old question, but to others searching for the REAL answer, here it is:
我知道这是一个老问题,但对于其他寻找真正答案的人来说,这里是:
The [L]flag DOESwork in .htaccessfiles. It tells the rewrite moduleto skip all of the following rules in that particular .htaccessfile. It does its job, Apache rewrites the url and exits the .htaccessfile.
该[L]标志确实在.htaccess文件中起作用。它告诉rewrite module跳过该特定.htaccess文件中的所有以下规则。它完成它的工作,Apache 重写 url 并退出.htaccess文件。
However, at the end of the .htaccessfile if the request url has been rewritten, the whole url matching process starts again with the new url.
但是,在.htaccess文件末尾,如果请求 url 已被重写,则整个 url 匹配过程将重新以新的 url 开始。
This is what happens above, ^(.*)$will always matchthe current url, it causes an infinite loop, only the maxredirectrewrite option (10 by default) stops it.
这就是上面发生的情况,^(.*)$将始终匹配当前 url,它会导致无限循环,只有maxredirect重写选项(默认为 10)会阻止它。
The !-ffile attribute test(as mentioned by the questioner) would solve the problem, since the url will match a real filename:
该!-f文件属性测试(由提问者提到的)会解决这个问题,因为该网址将匹配一个真实的文件名:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
重写规则 ^(.*)$ dispatch.fcgi/$1 [L,QSA]
now, if we request http://example.com/toappend, .htaccessrewrites it to dispatch.fcgi/toappendand no rewrite loopwill happen.
现在,如果我们请求http://example.com/toappend,将其.htaccess重写为dispatch.fcgi/toappend并且不会发生重写循环。
回答by pufos
Hy , add this after RewriteEngine On
Hy ,在后面加上这个 RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
.. and it should work stoping loops .
..它应该可以停止循环。
回答by Erik
Apparently -- and I only read this here, I have no first hand knowledge -- the [L] directive does not work in .htaccess files, only if its in your .conf file.
显然——我只在这里读到这个,我没有第一手知识——[L] 指令在 .htaccess 文件中不起作用,只有在你的 .conf 文件中。
See: Hidden features of mod_rewrite
within the .htaccess context, [L] will not force mod_rewrite to stop. it will continue to trigger internal
在 .htaccess 上下文中,[L] 不会强制 mod_rewrite 停止。它将继续触发内部
回答by CamaroSS
Faced the same problem, and it turns out that the best solution in Apache 2.3.9+ is to use ENDflag instead of Las it prevents mod_rewrite from looping over rules.
面临同样的问题,事实证明,Apache 2.3.9+ 中的最佳解决方案是使用END标志而不是L,因为它可以防止 mod_rewrite 循环遍历规则。

