Need Help with Mod Rewrite

Discussion in 'Programming' started by aklead, Mar 19, 2009.

  1. #1
    I have a quick question about using mod_rewrite. I've been reading up on it for the last few days and I was wondering what the proper approach would be for my situation.

    I have URLs that look like this:
    http://www.example.com/directory/index.php?product=212

    I want them to look like this:
    http://www.example.com/212/Product Name

    Now my page gets gets the variable "product" out of the URL and pulls information for that record from a database. Do I have to have a rewrite rule for every single product I have?

    OR... is there a way that I can have mod_rewrite only use the product number part of the URL and ignore anything else? So that I can write "/212/and anything i want here" ?

    Any help would be great!
     
    aklead, Mar 19, 2009 IP
  2. fightrsi

    fightrsi Guest

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi, try putting this in your .htaccess file.

    Pattern:
    ([0-9]+)/ will match a bunch of numbers followed by a forward slash.
    .* will match anything

    Substitution:
    directory/index.php?product=$1
    $1 is replaced with the numbers matched in the pattern between the ( )
     
    fightrsi, Mar 19, 2009 IP
  3. aklead

    aklead Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks fightrsi! That was exactly what I was looking for! Do you know if I would be able to use those URLs in my sitemap without any search bots throwing a fit about it?

    Thank again,
     
    aklead, Mar 19, 2009 IP
  4. fightrsi

    fightrsi Guest

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You're welcome. Yes, you can use the rewritten URL for your sitemap and anywhere else on your website or other people's website without any problems.
     
    fightrsi, Mar 19, 2009 IP