javascript hit counter Web World: FAQ: How do I create search engine friendly URLs?
Nikhil Sheth

Web World

This site is for php , mysql , flash,.... Inshort for any web related stuff

Wednesday, April 06, 2005

FAQ: How do I create search engine friendly URLs?

OK...let's say you wanted to make some search engine friendly URL's.

You have a script that is called as such:

--------------------------------------------------------------------------------
http://myhost.com/myscript.php?cat=45&id=12

--------------------------------------------------------------------------------

You want to make it look like:

--------------------------------------------------------------------------------
http://myhost.com/myscript/45/12.html

--------------------------------------------------------------------------------

First thing you need to do is recode any part of the script that makes the links. I.e., if you have this code:
--------------------------------------------------------------------------------

echo "$row[title]\n";
--------------------------------------------------------------------------------

you would make it:

--------------------------------------------------------------------------------

echo "$row[title]\n";

--------------------------------------------------------------------------------

Now, you need to add in your .htaccess file some lines to do the modrewrite:

--------------------------------------------------------------------------------
RewriteEngine on
RewriteBase /
RewriteRule ^myscript/([0-9]+)/([0-9]+)\.html$ /myscript.php?cat=$1&id=$2
--------------------------------------------------------------------------------

Those first couple of lines just set the stage for our modrewrite. The 3rd line is where the business is done. It says, "If a request for a URL comes in that is for /myscript/[atleast one number]/[atleast one number].html, then behind the scenes, redirect that to /myscript.php?cat=[the first number]&id=[the second number].

Simple eh?

5 Comments:

  • At 1:36 PM, Anonymous Yash said…

    Is there a way to do the same thing on a windows machine? .htaccess file won't work on a windows box. So are there any workarounds?

     
  • At 1:45 PM, Blogger Niks said…

    Here is a link which shows you the way to do in asp.net :

    http://www.codetoad.com/asp.net_ma_searchenginefriendly.asp
    http://www.stardeveloper.com/articles/display.html?article=2004022801&page=1

     
  • At 6:43 PM, Anonymous Yash said…

    Hey Thanks Niks. They were useful links.

     
  • At 4:00 PM, Blogger Dharmesh said…

    What about other links? This rewrite rule will change only myscript.php What about 10 such files?

     
  • At 4:12 PM, Blogger Niks said…

    Ever wondered how Google makes some of their pages have the extension .g? This is a fairly easy procedure really, and htaccess does a great deal in helpin us accomplish this. First decide what you want the new extension to be, for our purposes we will use the extension of .next (simply because it makes sense). Now we want all files with the .next extension to be treated as PHP files, so we will add the following line to our .htaccess file:

    AddHandler application/x-httpd-php .next

     

Post a Comment

<< Home