help need for url rewriting

Discussion in 'PHP' started by kcm4web, Jun 1, 2010.

  1. #1
    hello Gurus

    I need a help for URL Rewriting of a link

    product.php?catid=2&subcatid=Garden-Shed-Kits

    want to make

    product/Garden-Shed-Kits


    any body pls help me

    thanks in regards
     
    kcm4web, Jun 1, 2010 IP
  2. Boogy

    Boogy Active Member

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    73
    #2
    Boogy, Jun 1, 2010 IP
  3. dabaR

    dabaR Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well, URL rewriting is generally done with Apache's mod_rewrite module.

    There is no way to make product/Garden-Shed-Kits into product.php?catid=2&subcatid=Garden-Shed-Kits, because the 2 is missing. You could translate product/2/Garden-Shed-Kits into that though.

    Here's an example of a rewrite rule:
    
    <IfModule mod_rewrite.c>
    RewriteEngine On
    #Don't rewrite if the requested resource is a real file
    RewriteCond %{REQUEST_FILENAME} !-f 
    #Don't rewrite if the requested resource is a real directory
    RewriteCond %{REQUEST_FILENAME} !-d
    #Rewrite only if the request starts with 'product'
    RewriteCond $1 ^product
    RewriteRule RewriteRule ^product/([^/]*)/([^/]*)$ product.php?catid=$1&subcatid=$2 [L,QSA]
    </IfModule>
    Code (markup):
    The official reference is at http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

    Hope that helps!
    --
    Dan Bernardic
     
    Last edited: Jun 1, 2010
    dabaR, Jun 1, 2010 IP
  4. jsgt

    jsgt Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you still want to exclude the 2 from the "pretty" URL, you could let PHP handle the redirection by looking up which category the subcategory belongs to, assuming there isn't any ambiguity.

    So, building on dabaR's .htaccess:

    
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^product/ router.php
    
    PHP:
    In router.php you'd then check $_SERVER[ 'REQUEST_URI' ], extract the subcategory, look up which category the product is in, then set $_GET and $_REQUEST accordingly and include product.php. This gives you a lot more flexibility than having everything in the .htaccess file.
     
    jsgt, Jun 1, 2010 IP