Bangla Radio


Bookmark and Share

Wednesday, March 2, 2011

URL Rewrite

What are the benefits of rewriting URL?

Collected From:

When a search engine visits the dynamic url like product.php?id=5 it does not give much importance to that URL as search engine sees "?" sign treat it as a url which keeps on changing. so we're converting the dynamic URL like the product.php?id=5 to static url format like product-5.html. We'll rewrite the url in such a way that in browser's address bar it will display as a product-5.html but it actually calls the file product.php?id=5. So that why these kind of URL also named as SEO friendly URL.

What is required for URL rewriting??

To rewrite the URL you must have the mod_rewrite module must be loaded in apache server. And furthermore, FollowSymLinks options also need to be enabled otherwise you may encounter 500 Internal Sever Error.

How to check weather mod_rewrite module is enabled or not?
Data Collected:

Well there are lots of techniques to check this but I'll show you a very simple technique to check weather mod_rewrite module is enabled or not in you web server.

1) Type <?php phpinfo(); ?> in a php file and save it and run that file in the server.
2) And now you can the list of information, just search the word "mod_rewrite" from the browser's search menu
3) If it is found under the "Loaded Modules" section then this module is already loaded as you see in the picture below, otherwise you need to go to the next step for enabling mod_rewrite module.

How to enable mod_rewrite module in apache in xampp, wamp?

Now, I'll show you how to enable how to mod_rewrite module in apache installed under windows environment.
1) Find the "httpd.conf" file under the "conf" folder inside the Apache's installation folder.
2) Find the following line "#LoadModule rewrite_module modules/" in the "httpd.conf" file.You can do this easily by searching the keyword "mod_rewrite" from find menu.
3) Remove the "#" at the starting of the line, "#" represents that line is commented.
4) Now restart the apache server.
5) You can see now "mod_rewrite" in the Loaded Module section while doing "phpinfo()".

Examples of url rewriting for seo friendly URL

For rewriting the URL, you should create a .htaccess file in the root folder of your web directory. And have to put the following codes as your requirement.

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.php [nc]

The following example will rewrite the test.php to test.html i.e when a URL like http://localhost/test.htm is called in address bar it calls the file test.php. As you can see the regular expression in first part of the RewriteRule command and $1 represents the first regular expression of the part of the RewriteRule and [nc] means not case sensitive.

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ products.php?id=$1

The following example will rewrite the product.php?id=5 to porduct-5.html i.e when a URL like http://localhost/product-5.html calls product.php?id=5 automatically.

Hiding PHP file extension

Collected From:

Do you want to hide your web site's server script identity ? If you don't want to reveal the programming language ( server side script ) of your website to visitors of website so that any hacker or spammer will not be able to intrude or inject any code in your website.

Here is a small technique for you, you can use .html or .asp file to work as a php file i.e. use .asp or .html extension instead of .php. You just need to create a .htaccess file and put the following code in the .htaccess file. Remember that the .htaccess file should be placed in the root folder of your website.

# Make PHP code look like asp or perl code
AddType application/x-httpd-php .asp .pl

if you place the the above code in the .htaccess file then you can use contact.asp as the name of the file. Now a visitor thought that it is a ASP file but this file contains the codes of PHP.

You can put the following code in .htaccess file to work .htm or .html file as PHP file.

# Make all PHP code look like HTML
AddType application/x-httpd-php .htm .html


Redirecting www URL to non www URL

Data Collected From:

If you type in browser it will be redirected to


Add this Following Code:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^

RewriteRule (.*)$1 [R=301,L]



Rewriting '' to ''

My twitter profile its original link passing GET values ( but this URL is ugly in browser address bar, For user friendly we can change like this.


If you want change like this see the below code

RewriteEngine On

RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1

RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1




This time I'm talking about rewriting second parameter URLs using .htaccess file.

Collected From:




Original URL.

Rewriting Friendly URL.

.htaccess Code

//First Parameer
RewriteEngine On
^([a-zA-Z0-9_-]+)$ users.php?user=$1
^([a-zA-Z0-9_-]+)/$ users.php?user=$1

//Second Parameter
RewriteEngine On
^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?user=$1&page=$2
^([a-zA-Z0-9_-]+)/([0-9]+)/$ users.php?user=$1&page=$2








Post a Comment

Submit your Web Site to Search Engine

Google PR Check

Check Page Rank of your Web site pages instantly:

This page rank checking tool is powered by Page Rank Checker service

Keyword Based Search Engine Checker

Your domain:
Search on:  
Limit search to:
Keywords to search:

Powered by iWEBTOOL

Backlink Checker Tools

Your domain:

Powered by iWEBTOOL

Search Engine Spider Simulator

Enter URL to Spider

Google Penalty Checker

Google Penalty Checker

Powered by Li'l Engine's Google Penalty Checker