search friendly urls

Discussion in 'PHP' started by c4cyber, Oct 22, 2007.

  1. #1
    Please can anyone tell me THE SIMPLEST WAY to write search friendly urls

    in a very explantory way..
    I will be very thankful
     
    c4cyber, Oct 22, 2007 IP
  2. robca

    robca Well-Known Member

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    116
    #2
    using modrewrite (by .htaccess) if you do some research on google you will find out it is not so bad as you think to get the solution!
     
    robca, Oct 22, 2007 IP
  3. upl8t

    upl8t Peon

    Messages:
    80
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Imagine you have a script that when passed a category, prints out a list of books.

    /get-books-by-category.php?categoryid=123

    Now you want a search engine friendly url? First thing, in the script that prints out those urls, change the url to something like this:

    /category/horror/

    Then create a .htaccess file on your server to mod-rewrite those urls like this:

    RewriteRule category/([A-Za-z0-9_-])+/$ /get-books-by-category.php?category=$1 [L,QSA]

    This will rewrite those urls from /category/horror/ to /get-books-by-category.php?category=horror

    In your get-books-by-category.php script, you grab the category name from the querystring, look it up in the database to get the category id, then continue as usual.

    SELECT CategoryId FROM Categories WHERE CategoryName = 'horror'

    Now this adds an additional query every page, so their are a couple solutions to this. First, cache the look ups, of the categoryname to categoryid, or actually put the category id in the url.

    Using a url like this:

    /category/5/horror/ you can put the id in the url, and not have to look it up.

    The mod rewrite then becomes:

    RewriteRule category/([0-9]+)/([A-Za-z0-9_-])+/$ /get-books-by-category.php?categoryid=$1&categoryname=$2 [L,QSA]

    This captures the id from the url and the name (optional), passing that id to the script.
     
    upl8t, Oct 22, 2007 IP
  4. c4cyber

    c4cyber Well-Known Member

    Messages:
    1,040
    Likes Received:
    27
    Best Answers:
    1
    Trophy Points:
    150
    #4
    thanks upl8t !
     
    c4cyber, Oct 22, 2007 IP