Useful url rewriting examples using .htaccess

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.

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

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

To have main keywords in the URL is always preferred and recommended SEO technique. In the following URL rewrite you can display the name of the product in URL. Note that id and keywords (s) are passed as GET vars.

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

3) Redirecting non www URL to www URL

I want that if you type inimist.com in browser you should be redirected to www.inimist.com. If you want to do same with your website then put the following code to .htaccess file. To know more on benefit of this kind of redirection please check the post about SEO friendly redirect (301) redirect in php and .htaccess.

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

4) Rewriting yoursite.com/inimist.php?username=arvind to inimist.com/arvind

Most of the user based social networking websites like to show user profiles this way. For example my profile at facebook is at https://www.facebook.com/arvindkthakur.jnr. How do they do it. To have a  same kind of redirection i.e http://yoursite.com/abc to http://yoursite.com/user.php?username=abc you can add the following code to the .htaccess file.

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

5) Redirecting the domain to a  subfolder of inside public_html.

Suppose that you are redeveloping your website and all the development is being performed inside the “dev” folder which is placed at root. So, in this case development version of the website can be accessed like “inimist.com/dev”.  Now you want to show the development website when accessed at http://www.inimist.com. To achieve you just need to place the  following code inside the .htaccess file in the root folder of the website. Placing it, www.inimist.com should point to the files inside “dev” folder.

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

6) Redirecting to a product page of a category or sub-category

Suppose we have a category/product kind of website. There are unlimited levels of categories. We may have urls in the following formats:

[code]http://www.mystoreabc.com/store/cat1/p-101-product-name
http://www.mystoreabc.com/store/cat1/subcat1/p-101-product-name
http://www.mystoreabc.com/store/cat1/subcat1/p-sub-subcat1/101-product-name[/code]

and so on. To make it easier to identify product in the url we would need to separate it from rest of url by some token or a set of letters.. In my case, i would separate it by “p-” as shown in the URLs above. We will create a rewrite url now. Here’s it:

[code]RewriteRule ^store/([^\.]+)/p-([0-9]+)-(.*)+/?$ index.php?path=$1&p=1231 [L][/code]

In the index.php, print the path and p variables, as in:

[code]The requested page was: <?php echo $_GET[‘page’]; ?>, <?php echo $_GET[‘pid’]; ?>[/code]

to get result for the url, for example http://www.mystoreabc.com/store/software/desktop/id-1231-weather-widget-for-your-desktop, as:

[code]The requested page was: software/desktop, 1231[/code]

One thought on “Useful url rewriting examples using .htaccess

Leave a Reply