Url Rewriting with .htaccess file

Now, Its time to clean up our url. It is very important to have keywords in the url so that search engines can tackle them easily. Further Rewriting urls makes it more readable and it looks very clean.

We can rewrite our urls using .htaccess file. So let us start with

What is .htaccess file?

It is a common file used by several web servers to control configuration for a directory. Also known as hypertextaccess. It is an extremely sensitive file.

A missing semi colon, incorrect letter or an extra backslash can mess everything up. So, be careful while working with this file.

To achieve this we have to follow these steps :
 
1. Create a .htaccess file. Save this file in root directory.
2. Enable the RewriteEngine with this code

        RewriteEngine on
 
3. Now define RewriteRule for the configuration

       RewriteRule ^product-([^/]+)\.html$ product.php?category=$1

Syntax : RewriteRule INPUTURL OUTPUTURL [FLAGS]

 RewriteEngine on
 RewriteRule ^product-([^/]+).\html$ product.php?category=$1


Above code will result following url -

Original Url    : http://example.com/product.php?category=car
After Rewriting : http://example.com/product-car.html

We can also make our urls look like directories as follows.
 
 RewriteEngine on
 RewriteRule ^product/([^/]+).\html$ product.php?category=$1


Wait... One thing to note : Always use absolute paths for your links while dealing with directory type url rewriting.
Url Rewriting with .htaccess file Url Rewriting with .htaccess file Reviewed by JS Pixels on June 13, 2011 Rating: 5

No comments:

Altaf Web. Powered by Blogger.