Armel Larcier
Armel Larcier

Reputation: 16037

htaccess RewriteRule conflict : how does RewriteCond work?

I've been trying and failing all day long yesterday.

I need to rewrite all urls to pass them as parameters to my front controller.

But I also need all urls starting with "admin" to be rewritten and passed as parameters to my admin controller...

Here is the code I tried. I know it can't work as is but I can't figure out how to do it...

RewriteRule ^(.*)$ /index.php?uri=$1 [L]
RewriteCond $1 !^/admin
RewriteRule ^admin/(.*)$ /admin/index.php?uri=$1 [L]

If someone can help me I'll be infinitely grateful.

Upvotes: 0

Views: 1336

Answers (1)

Chelsea Urquhart
Chelsea Urquhart

Reputation: 1428

Put your RewriteCond before RewriteRule or put your admin rewrite before everything else.

As it stands right now, a visitor going to /admin is matched by the first rewrite rule and then that's it.

Your RewriteCond is actually applying to the second RewriteRule because of its position.

I would also change $1 to %{REQUEST_URI} and add QSA to the flags so query strings are passed properly.

RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^(.*)$ /index.php?uri=$1 [L,QSA]
RewriteRule ^admin/(.*)$ /admin/index.php?uri=$1 [L,QSA]

This should work.

Upvotes: 1

Related Questions