#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Location
    San Diego, CA
    Posts
    57
    Rep Power
    34

    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:

    Code:
    /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:

    Code:
    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:

    Code:
    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!
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,120
    Rep Power
    9398
    Your second rule seems... off...
    Code:
    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.
    Last edited by requinix; October 1st, 2008 at 12:10 AM.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Location
    San Diego, CA
    Posts
    57
    Rep Power
    34
    Originally Posted by requinix
    Your second rule seems... off...
    Code:
    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!

IMN logo majestic logo threadwatch logo seochat tools logo