October 1st, 2008, 12:23 AM
Match anything except a certain string
I'm using Apache's mod_rewrite to do some internal URL redirection on a blog engine I'm making.
This is the behavior I want:
I can get it all working fine if that second line is left out with these statements:
/blog/ <-- viewing the blog
/blog/post/ <-- form to make a new post
/blog/x/ <-- redirects to /blog/index.php?title=x
/blog/tag/y/ <-- redirects to /blog/index.php?tag=y
But when I want to make post/ the exception to the second rule, I'm stumped. I need to change the second statement to this pseduocode:
RewriteRule ^tag/(.*)/$ /blog/index.php?tag=$1
RewriteRule ^(?<!tag/)(.*)/$ /blog/index.php?title=$1
Match anything as long as it's not post/ or preceded by tag/
This was my attempt at that logic:
That successfully makes post/ an exception, but /blog/tag/y ends up as /blog/index.php?title=/blog/index.php/y
RewriteRule ^(?<!tag/)((?!post/).*)/$ /blog/index.php?title=$1
What is the correct way to write this? Thanks in advance!
October 1st, 2008, 01:07 AM
Your second rule seems... off...
Tip: the [L]ast flag tells Apache to stop processing other rules.
RewriteRule ^tag/(.*)/$ index.php?tag=$1 [L]
RewriteRule ^(?!post/)(.*)/$ index.php?title=$1
 Apparently you want trailing /s. Personally I don't like enforcing them, but whatever.
Last edited by requinix; October 1st, 2008 at 01:10 AM.
October 1st, 2008, 01:17 AM
That worked perfectly! Thank you!
Originally Posted by requinix