URL redirect through .htaccess file

URL rewrite helps you to hide your extension name or show different extension name like index.php can be displayed as index.html or only index or home etc.

The following code will rewrite anyfile.php to anyfile.html
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php

The following code will rewrite index.php to index
RewriteEngine on
RewriteRule ^index index.php

Few Example to create SEO friendly URL.

1. Rewrite product.php?id=1 to product-1.html

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

2. Rewrite product.php?id=12 to product/cat/12.html

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

3. Rewrite domain.com/member.php?username=arup to domain.com/arup

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

4. Rewrite domain.com/users.php?id=username&page=2 to domain.com/username/2

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

Comments
06 February
2014
pooja
My url is http://abc.pqr.xyz.com/folder1/page.php when i hit the url i want to display content from pqr.xyz.com folder

Please help me to do this task using .htaccess file.

Thanks.
05 January
2014
shimpy
how can i change mydomain.com/www.php/anyurl.com to mydomain.com/www/anyurl.com in htaccess file
16 December
2013
Hasdeep
Hi, can you help me. I have a webpage by the name of page.php?id=1 and multiple dynamic links. I have written the following code in htaccess file:
RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^$ /page.php%1? [R=301,L]
RewriteRule ^([0-9]+)$ page.php?id=$1 [L]
Now, the problem is example.com/page.php?id=1 and example.com/1 both links are opening. I am not able to redirect automatically example.com/page.php?id=1 to example.com/1
comments powered by Disqus