Skip navigation

What is mod_rewrite?

mod_rewrite is a Apache module used for manipulating (rewriting) requested URL’s on the fly. This process uses a rule based rewrite engine (regular expression parser is used).

Using mod_rewrite

Apache directives are created to tell mod_rewrite what you want to do. These Directives are configurations, often used in the httpd.conf (server context) or .htaccess (directory context).

RewriteEngine : using this mod_rewrite engine is turned On/Off.

RewriteRule : Creates a single rule for rewriting the URL. The order of creation is important, because this order is applied at run-time processing.

Code

RewriteEngine On
RewriteRule ^sample\.html$ anotherSample.html

RewriteEngine On

Turns on mod_rewrite engine.

RewriteRule ^sample\.html$ anotherSample.html

URL starting with sample.html is changed to anotherSample.html

Info

^sample\.html$ is a regular expression where ^ is starting of the url excluding domain name, followed by sample, then by \.html (“.” is escaped ), and $ end of string.

This example shows the content of the anotherSample.html page when the url is http://sample.html.