.htaccess RewriteRule Examples

Here are some useful mod_rewrite RewriteRule redirect examples that you can use in your .htaccess file.

You can also visit non-www to www over http and https RewriteRule example and our simple .htaccess 301 Redirect Generator.

 

Example 1

Original URL:
http://www.domain.com/product.php?id=15

Rewritten URL:
http://www.domain.com/15.php

Rule for .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)\.php$ /product.php?id=$1 [L]

 

Example 2

Original URL:
http://www.domain.com/product.php?id=15

Rewritten URL:
http://www.domain.com/product15.php

Rule for .htaccess:
RewriteEngine On
RewriteRule ^product([^/]*)\.php$ /product.php?id=$1 [L]

 

Example 3

Original URL:
http://www.domain.com/product.php?cat=5&id=15

Rewritten URL:
http://www.domain.com/5/15.php

Rule for .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.php$ /product.php?cat=$1&id=$2 [L]

 

Example 4

Original URL:
http://www.domain.com/product.php?cat=5&id=15

Rewritten URL:
http://www.domain.com/5-15.php

Rule for .htaccess:
RewriteEngine On
RewriteRule ^([^-]*)-([^-]*)\.php$ /product.php?cat=$1&id=$2 [L]

 

Example 5

Original URL:
http://www.domain.com/product.php?category=vehicles&product=bus

Rewritten URL:
http://www.domain.com/product/vehicles/bus.php

Rule for .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.php$ /product.php?category=$1&product=$2 [L]

 

Example 6

Original URL:
http://www.domain.com/cgi-bin/shop.php?cmd=product&category=vehicles&product=bus

Rewritten URL:
http://www.domain.com/product/vehicles/bus.php

Rule for .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.php$ /cgi-bin/shop.php?cmd=$1&category=$2&product=$3 [L]

 

Example 7

Original URL:
http://www.domain.com/cgi-bin/shop.php?cmd=product&category=vehicles&product=bus

Rewritten URL:
http://www.domain.com/shop/product/vehicles/bus.php

Rule for .htaccess:
RewriteEngine On
RewriteRule ^shop/([^/]*)/([^/]*)/([^/]*)\.php$ /cgi-bin/shop.php?cmd=$1&category=$2&product=$3 [L]

 

Example 8

Original URL:
http://www.domain.com/cgi-bin/shop.php?cmd=product&category=vehicles&product=bus

Rewritten URL:
http://www.domain.com/shop-vehicles-bus.php

Rule for .htaccess:
RewriteEngine On
RewriteRule ^shop-([^-]*)-([^-]*)\.php$ /cgi-bin/shop.php?cmd=product&category=$1&product=$2 [R=301,L]

 

Example 9
The following rewrite rule utilizes ^$ to represent the root and rewrite that to your /shop directory.

Original URL:
http://www.domain.com/

Rewritten URL:
http://www.domain.com/shop

Rule for .htaccess:
RewriteEngine On
RewriteRule ^$ /shop [R=301,L]

 

General Notes

[L] - Stops any later rewrite rules from affecting this URL.
[R=301,L] - Performs a 301 redirect and also stops any later rewrite rules from affecting this URL.

  • 162 Users Found This Useful
Was this answer helpful?

Related Articles

Do you support mod_rewrite for apache?

Yes, mod_rewrite is enabled on your server. For more information on what mod_rewrite is and how...

How to execute php code as .htm or .html files

In your .htaccess file insert this line of code. addhandler application/x-httpd-php .htm .html

Disabling Magic Quotes GPC

How to turn off magic quotes gpc (required by Joomla 3 and some other scripts).1. Create a...

Block Bad Bots and Spiders using .htaccess

Below is a useful code block for blocking a lot of the known bad bots and site rippers currently...

Redirect non-www to www over http and https

Use the following .htaccess code to redirect non-www URLs to www over http and https. Create, or...