Helpful Information
 
 
Category: Regex Programming
Match anything except a certain string

Hi all,

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:


/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

I can get it all working fine if that second line is left out with these statements:


RewriteRule ^tag/(.*)/$ /blog/index.php?tag=$1
RewriteRule ^(?<!tag/)(.*)/$ /blog/index.php?title=$1

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:

Match anything as long as it's not post/ or preceded by tag/

This was my attempt at that logic:


RewriteRule ^(?<!tag/)((?!post/).*)/$ /blog/index.php?title=$1

That successfully makes post/ an exception, but /blog/tag/y ends up as /blog/index.php?title=/blog/index.php/y

What is the correct way to write this? Thanks in advance!

Your second rule seems... off...

RewriteRule ^tag/(.*)/$ index.php?tag=$1 [L]
RewriteRule ^(?!post/)(.*)/$ index.php?title=$1
Tip: the [L]ast flag tells Apache to stop processing other rules.

[edit] Apparently you want trailing /s. Personally I don't like enforcing them, but whatever.

Your second rule seems... off...

RewriteRule ^tag/(.*)/$ index.php?tag=$1 [L]
RewriteRule ^(?!post/)(.*)/$ index.php?title=$1
Tip: the [L]ast flag tells Apache to stop processing other rules.

[edit] Apparently you want trailing /s. Personally I don't like enforcing them, but whatever.

That worked perfectly! Thank you!










privacy (GDPR)