How to do Search engine Friendly URL through htaccess ReWriteRULE

PHP is the vast programming used for developing websites. You might have seen hundreds of website URL having with a lot of query string in it.

For Example:

http://babynames.agurchand.com/index.php?origin=African&gender=male&type=chaldean&page=4

If you see the above URL it looks ugly and with a lot of ampersand (&) symbols.
If you go for a The W3C Markup Validation test you can see hundreds of errors threw by W3C Validation Service. This is only because of this ampersand symbol, well most of the searching engines except Google won’t index the page if the URL has any character like (? &) question mark and ampersand. So it is always to make a clean human friendly and search friendly URLs.

If you look at the below Example the URL is search engine friendly and also human friendly.
Why I’m saying human friendly here means anyone can easily remember the below URL when comparing the above messy URL.

http://babynames.agurchand.com/African/male/chaldean/2
Now lets see how to remove query strings and make a clean search friendly URL for your website.

As i said earlier you can access the example website in two ways.

TYPE I:

http://babynames.agurchand.com/index.php?origin=African&gender=male&type=chaldean&page=2

TYPE II:

http://babynames.agurchand.com/African/male/chaldean/2

To achieve the TYPE II kind of URL you have to write or edit your (.htaccess) file. Htaccess file you can find in your root directory of a website or you can create your own file with the help of any text editor like notepad++

Here is the piece of htaccess code used in my website:

 

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-zA-Z0-9+-()]+)/?$ /index.php?origin=$1
RewriteRule ^([a-zA-Z0-9+-()]+)/([a-zA-Z0-9+-]+)/?$ /index.php?origin=$1&gender=$2
RewriteRule ^([a-zA-Z0-9+-()]+)/([a-zA-Z0-9+-]+)/([a-zA-Z0-9+-]+)/?$ /index.php?origin=$1&gender=$2&type=$3
RewriteRule ^([a-zA-Z0-9+-()]+)/([a-zA-Z0-9+-]+)/([a-zA-Z0-9+-]+)/([a-zA-Z0-9+-]+)/?$ /index.php?origin=$1&gender=$2&type=$3&page=$4
</IfModule>

 

 

How is this working?

Let's take one more example of an URL.

EXAMPLE 1:

If your URL structure is like below

http://www.playvideos.in/video?id=10

So the rewrite rule should be like this:

RewriteRule ^([a-zA-Z0-9-]+)/?$ /video.php?id=$1

 

EXAMPLE 2:

http://www.playvideos.in/shortfilms/video.php?id=373

So the rewrite rule should be like this:

RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /$1/video.php?id=$2

 

Have a nice clean URL for your website from today!.

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *