Monday, April 11, 2011

HTACCESS CODING

Few days ago I have found one of my drawbacks of my PHP coding. My web programs fails about 40% of SEO friendliness because of dynamic urls of products like "kolorob.info/index.php?id=45". I got realized that I have to use more SEO friendly url structure like "kolorob.info/page/2". I could not find any way to make this. I searched google and asked my friends, but couldn't get any information. A week ago, I was continuing that discussion with Muntasir Rashid at our intro6's temporary tent and Tarif Hossain joined there and gave me the legendary information about .htaccess. He said that I can solve this problem by this. And at last I have solved the problem.

Now I am going to discuss about this problem.

Problem: Redirect a unknown url address to a known url address.

Description of the Problem: When one write an address in the browser's address bar it shows some information from the file belongs in that address. like when I write in address bar "http://kolorob.info/kolorob/" or "http://kolorob.info/kolorob/index.php" it can show the specific page because this page is physically or dynamically exists. But when I write "http://kolorob.info/kolorob/post/26" browser starts to find a folder named "26" from another parent folder "post". But these folders are not exist physically or dynamically. If I want to show the 26th post to browser I need to get the information from the url to any of my existing page. I need the type of material ("post") and the number of that type of material to show the user.

Solution:
Need to add the following line to the .htaccess file of the folder where the project is situated.

RewriteEngine on

RewriteRule ^(./*)$ index.php [QSA,L]

The code shown above will help the url like ("http://kolorob.info/kolorob/post/26") to be redirected to "http://kolorob.info/kolorob/index.php" and it will bring the asked url by a global variable $_SERVER['REQUEST_URI']. Now one can easily get the whole url that was requested by user in "index.php". This url could be broken into pieces with the explode function of PHP using delimiter ("/"). Then the pieces are found "post", and "26". Index.php can easily recognize the type of material ("post") to be shown and the number of that type of material (26th post).