0

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.

2 Answers2

4

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 ...

yunzen
  • 32,854
  • 11
  • 73
  • 106
scibuff
  • 13,377
  • 2
  • 27
  • 30
0

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]
Community
  • 1
  • 1
LazyOne
  • 158,824
  • 45
  • 388
  • 391