Useful url rewriting examples using .htaccess

Useful url rewriting examples using .htaccess

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.

Now let’s look at the examples

1)Rewriting product.php?id=12 to product-12.html

It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.

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

2) Rewriting product.php?id=12 to product/ipod-nano/12.html

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

3) Redirecting non www URL to www URL

If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]

4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz

Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e 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

Related Posts


5 thoughts on “Useful url rewriting examples using .htaccess

  1. Alaine Tuamoheloa

    Hi, Neat post. There’s a problem with your web site in internet explorer, would check this… IE still is the market leader and a big portion of people will miss your wonderful writing due to this problem.

    Reply
  2. kore dizileri izle

    i bookmarked you in my browser admin thank you a lot i will probably be in search of your up coming posts

    Reply
  3. Alva Stavros

    I simply want to mention I am very new to blogging and site-building and truly liked you’re web site. More than likely I’m likely to bookmark your site . You definitely come with fantastic articles and reviews. Appreciate it for revealing your webpage.

    Reply
  4. Rutha Dwight

    Thanks for your your content – really interesting. We love our Kindle and the best site for tips and tricks is coolkindles.

    Reply

Leave a Reply

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


*