Using mod_rewrite to convert dynamic URLs to SEO friendly URLs

SEO friendly URLs  (sometimes also known as Semantic URL or clean URL )  are intended to improve the readability and accessibility of the website. Nowadays these URLs play an important role improving the page rank. Below is the difference between a semantic and non-semantic URLs.

Non-Semantic URL Semantic URL
http://www.xyz.com/product.php?id=34 http://www.xyz.com/product/34
http://www.xyz.com/index.php?page=name http://www.xyz.com/name

It is very easy to create semantic URLs using mod_rewrite. So let’s jump into it

STEP 1: Create a file named index.php

Create the file write the following code.

screenshot-39

STEP 2: Create a file named user.php

Create the file for redirection purpose.

screenshot-39

STEP 3. Create a file named .htaccess

Create the file named .htaccess. You may run into problems while creating this file as it doesn’t have any name.  So all you have to is to save the file by name

“.htaccess”

(including the quotes).

Now use mod_rewrite in your .htaccess file to parse out the elements of the URL. Like this:

screenshot-38

The line starting with the # symbol are the comments. Line after the first comment is used to remove the .php extension from the file name. In last  line the ([^\.]+) pulls the elements out and puts them in variables $1.

Now run the file in your favorite browser.

screenshot-41

Click on any link and see the magic. You can see that URLs are now semantic and easy to read.

screenshot-42 Hope this is useful. If you have any doubts then please do comment in the section below. I will try my best to answer the questions.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s