URL Rewriting using .htaccess in PHP

Hello friends .Do you know what is URL rewriting in PHP ?

This post will help you about URL rewriting.

Static URLs are known to be better than Dynamic URLs because of a number of reasons
1. Static URLs typically Rank better in Search Engines.
2. Search Engines are known to index the content of dynamic pages a lot slower compared to static pages.
3. Static URLs are always more friendlier looking to the End Users.

What is the benefits of rewriting URL?

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.

If you are looking for the examples of URL rewriting then this post might be useful for you. In this post, I’ve given five useful examples of URL rewriting using .htacess.

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.

SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.

RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2

If you like to do like http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.

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

Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1

I think this post become helpful to those who are new for URL Rewriting amd .htaccess in PHP.

Please give any comment about this post .If you have any question or problem in PHP then comment me. I will try my best.

Thanks !!!!

  • David Barker

    Very useful! Thanks, will start using these in some of my projects now I realise a) it can be done, and b) it’s not as scary as I imagined! :-)

  • David Barker

    Very useful! Thanks, will start using these in some of my projects now I realise a) it can be done, and b) it’s not as scary as I imagined! :-)

  • Kevin Glen

    Informative article to those not familiar with mod_rewrite rules in an .htaccess file but this is not “in PHP”. It’s actually in “mod_rewrite syntax”.

  • Kevin Glen

    Informative article to those not familiar with mod_rewrite rules in an .htaccess file but this is not “in PHP”. It’s actually in “mod_rewrite syntax”.

  • Pingback: 網站製作學習誌 » [Web] 連結分享

  • http://roshanbh.com.np/ Roshan

    great article dude…but seems that you liked the examples of my blog post which are ditto here….

    http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html

  • http://roshanbh.com.np Roshan

    great article dude…but seems that you liked the examples of my blog post which are ditto here….

    http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html

  • admin

    I am a professional web developer since last 1 year.I use Url rewriting in my many projects and i prepare one note about URL rewriting. These examples are from my note. It’s not copied from any site or blog.

  • admin

    I am a professional web developer since last 1 year.I use Url rewriting in my many projects and i prepare one note about URL rewriting. These examples are from my note. It’s not copied from any site or blog.

  • http://www.jamesradford.net/ James Radford

    I use htaccess url rewriting for all my PHP projects. Nice article.

  • http://www.jamesradford.net/ James Radford

    I use htaccess url rewriting for all my PHP projects. Nice article.

  • Anandhan

    Hi i am beginner in php ,

    i have implement the following lines of code to make .htaccess work .

    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9_-]+)$ userview.php?uid=$1
    RewriteRule ^([a-zA-Z0-9_-]+)/$ userview.php?uid=$1

    To display usereview page as when i click the userid in the page test.php i needs to be look like this http://localhost/mytest/userview/1

    So please help me out to trigger which method is correct one

  • Hemendra

    Thanks