mod_rewrite seems to ignore [L] flag

后端 未结 2 2011
一整个雨季
一整个雨季 2021-01-15 01:50

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         


        
2条回答
  •  太阳男子
    2021-01-15 02:32

    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]
    

    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
    

    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.

    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]
    

    or:

    RewriteCond %{ENV:REDIRECT_STATUS} !200
    RewriteCond $1 !^(index\.php|admin|assets|images|uploads|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]
    

提交回复
热议问题