My first stab at mod_rewrite - need practical advice

Discussion in 'Apache' started by alext, Aug 28, 2005.

  1. #1
    First off this is doing the job that I want it to. Namely, user enters:

    --) mydomain.com/stuff/cat/tech/linux

    and it becomes the equivalent of

    --) mydomain.com/stuff/news.php?cat=tech&sub_cat=linux

    My .htaccess is located in the sub-directory "stuff"
    
    <IfModule mod_rewrite.c>
     RewriteEngine on
     RewriteRule cat/(.*)/(.*)$ news.php?cat=$1&sub_cat=$2
    </IfModule>
    
    Code (markup):
    Tested, works, can't find a problem. However being the paranoid programmer type...

    cat/(.*)/(.*)$

    Finally the question:
    Since I am not starting with a ^, are there considerations and/or unintended consequences with this? I guess that the exp will match any string ending with "cat/anything/anything" and how could it start with anything except "mydomain.com/stuff/" since the .htaccess is located there? Is there some best practice I am missing?

    There isn't enough coffee in my bloodstream, so I hope this made sense.

    TIA
     
    alext, Aug 28, 2005 IP
  2. expat

    expat Stranger from a far land

    Messages:
    873
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    expat, Aug 28, 2005 IP
  3. alext

    alext Active Member

    Messages:
    406
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    68
    #3
    Thanks for the link and info. I tried numerous variations, but ended up almost back where I started from. I kept getting 500's when I tried [NC] and a-z rather than a-zA-Z, but no big deal.

    
    <IfModule mod_rewrite.c>
     RewriteEngine on
     RewriteRule ^cat/([a-zA-Z]+)/([a-zA-Z]*)$ news.php?cat=$1&sub_cat=$2 [L]
    </IfModule>
    
    Code (markup):
    It is more exacting, so I am happier. I also see now how I can extend it to do some other things!

    :)
     
    alext, Aug 28, 2005 IP
  4. expat

    expat Stranger from a far land

    Messages:
    873
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    expat, Aug 28, 2005 IP