Andrea
Andrea

Reputation: 45

mod_rewrite seems to ignore [L] flag

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]

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)

Upvotes: 2

Views: 640

Answers (2)

Mike Brant
Mike Brant

Reputation: 71384

Your second rule is strange. I do not know what you are attempting to do by putting $1 as the value you are checking your condition against. It should probably look like this:

RewriteEngine on

RewriteRule ^admin/(.*) /backend.php/$1 [L]

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

Note I have also added a pass-through for backend.php

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143896

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]

Upvotes: 2

Related Questions