mod_rewrite的似乎忽略[L]标志 [英] mod_rewrite seems to ignore [L] flag
问题描述
我试图使用 [L]
标志重写规则
,但它似乎没有不工作。我想,如果你可以在页面:
I'm trying to use the [L]
flag in RewriteRule
, but it doesn't seem to work. I'd like that if you call the page:
www.domain.com/admin/
这将您重定向到:
it redirects you to:
www.domain.com/backend.php
否则,如果调用任何其他网页(除了某些页)重定向到:
Otherwise, if you call any other page (except for some pages) it redirects to:
www.domain.com/index.php
下面是我到目前为止有:
Here is what I have so far:
RewriteEngine on
RewriteRule ^admin/(.*) /backend.php/$1 [L]
RewriteCond $1 !^(index\.php|admin|assets|images|uploads|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
如果我只用了第一条规则,它的工作原理(但显然不会重定向其他页面的index.php)。如果我添加第二个规则,服务器似乎忽略第一个规则(我认为它是覆盖的第二条规则,即使我添加了 [L]
标志)
If I use only the first rule, it works (but obviously doesn't redirect other pages to index.php). If I add the second rule, the server seems to ignore the first rule (I think it is "overwritten" by the second rule, even if I added the [L]
flag)
推荐答案
这是不是如何→
的作品。重写引擎将通过所有的规则,直到该URI进入发动机不断循环是相同的所述一个出来。在→
只是告诉重写引擎停止应用规则的在当前循环迭代的。所以说你有:
This isn't how L
works. The rewrite engine will continually loop through all the rules until the URI going into the engine is the same as the one coming out. The L
just tells the rewrite engine to stop applying rules in the current loop iteration. So say you have:
RewriteRule ^(.*)$ /foo/$1 [L]
RewriteRule ^(.*)$ /bar/$1 [L]
在1次迭代,给出的URI /等等
,我得到 /富/等等
,因为它停止后重写第一条规则(但仍将继续循环)。如果我删除→
:
after 1 iteration, given the URI /blah
, I get /foo/blah
because it stops rewriting after the first rule (but will still continue to loop). If I remove the L
:
RewriteRule ^(.*)$ /foo/$1
RewriteRule ^(.*)$ /bar/$1
经过1次迭代,给出的URI /等等
,我得到 /酒吧/富/等等
。这两个规则得到应用,一前一后,因为→
不存在停止它。
after 1 iteration, given the URI /blah
, I get /bar/foo/blah
. Both rules get applied, one after the other because the L
isn't there to stop it.
您需要从此改写首先在你的第二个规则,以prevent它添加一个条件,无论这些人会做的事:
You need to add a condition in your second rule to prevent it from rewriting the first, either one of these will do:
RewriteCond $1 !^(backend\.php|index\.php|admin|assets|images|uploads|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
或
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond $1 !^(index\.php|admin|assets|images|uploads|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
这篇关于mod_rewrite的似乎忽略[L]标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!