user1351104
user1351104

Reputation:

How can I use an exception to a RewriteRule redirect?

I have the following in my .htaccess file

RewriteRule ^post/(.*)$ index.php?post=$1 [NC]
RewriteRule ^(.*)$ index.php?page=$1 [NC]

I expect it to rewrite (for example) mysite.com/x to mysite.com/index.php?page=x, with the exception of mysite.com/post/x rewritten to mysite.com/index.php?post=x, however it doesn't work.

Upvotes: 0

Views: 193

Answers (2)

LazyOne
LazyOne

Reputation: 165148

Just adding [L] flag will not be enough, as rewriting goes in cycles until no more rewrites occurs.

RewriteRule Last [L] flag not working?


There are quite a few approaches -- here is one of them (possibly the best one considering your example rules).

# do not do anything for index.php (already rewritten URLs)
RewriteRule ^index\.php$ - [L]

# my rewrite rules
RewriteRule ^post/(.*)$ index.php?post=$1 [NC,L]
RewriteRule ^(.*)$ index.php?page=$1 [NC,L]

Upvotes: 0

scibuff
scibuff

Reputation: 13755

add the L directive, e.g. RewriteRule ^post/(.*)$ index.php?post=$1 [L,NC]

L meaning last, i.e. if the rewrite rule is matched it will stop processing the next rules ...

Upvotes: 4

Related Questions