Using rewrite rules
mod_rewrite is a module that uses a rule-based rewriting engine (based on a regular-expression parser) to rewrite requested URLs on the fly. The *very* basic examples presented here will redirect a browser trying to access a website to another address on the server. Mod_rewrite has many other implementations that will not be covered in this document.
DETAILS:
The authoritative source for mod_rewrite related material is http://httpd.apache.org/docs/mod/mod_rewrite.html. Please use that reference for any in-depth questions. mod_rewrite uses .htaccess files. For more help with .htaccess files refer to our KB article What is an .htaccess file.
This article uses the example 'mt-example.com' which should always be substituted with your actual domain name.
Example 1:
This example will redirect requests for http://mt-example.com/request/ to http://mt-example.com/redirect/
Setup:
- FTP to your website and go into the web directory:
(ss) Shared Server: /var/www/html/
(gs) Grid-Service: /home/#####/domains/mt-example.com/html/
(dv) Dedicated-Virtual Server: /var/www/vhosts/mt-example.com/httpdocs/ - Create a directory called 'request' -- this will be accessible via http://mt-example.com/request/
- Create a directory called 'redirect' -- this will be accessible via http://mt-example.com/redirect/
Enter the 'request' dir and create an .htaccess file with the following text:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^.*$ /redirect/ [R]
and save it. We'll get into what these statements mean later.
Go back a directory and enter the directory named 'redirect', create a file called index.html with the content of your choosing, for example:
<html>
<body>
Mod_rewrite is working!
</body>
</html>
Just about any html page named index.html will work.
Execution:
Use a browser to go to http://mt-example.com/request/ If mod_rewrite is working you'll see that the browser has automatically been redirected to http://mt-example.com/redirect/
How it works:
When a browser requests a document the webserver (apache) looks through any .htaccess files in the current directory and above for rules to follow. In this example we have turned on mod_rewrite with the following lines:
Options +FollowSymLinks
RewriteEngine On
The first line turns on an important apache directive related to how mod_rewrite works and the second explicitly turns on mod_rewrite.
The last line is the part that tells mod_rewrite what to look for and what to do. 'RewriteRule' tells mod_rewrite to define a rule set.
The part of the line with strange symbols is possibly the trickiest portion and is known as a regular expression (regex)
TIP:
More details on regular expressions can be found here: The syntax of this line is "a url that matches THIS rule set should be redirected HERE."
- THIS is defined by ^.*$
- ^ Stands for "whatever is after me is at the beginning of a line"
- . Stands for "any non-whitespace character" (A b i 9 7 are all examples)
- * Stands for "whatever is before me can match zero or more times"
- $ Stands for "whatever is before me comes at the end of a line"
So this part becomes "match anything at the beginning of the line any number of times until the end of a line" which in plain English means "match anything and everything" http://mt-example.com/request/werwqerasdfas will match this rule just as well as http://mt-example.com/request/ and http://mt-example.com/request/another-directory/index.html
Basically any request sent to this directory or anything below it will be caught by the regular expression
HERE is defined by /redirect/ and is just the path from the webroot to the target. If we wanted to redirect to http://mt-example.com/redirect/something-else/ this part would read
/redirect/something-else/
The last part of the line '[R]' tells mod_rewrite that this is a redirect.
Example 2:
This example will redirect requests for http://mt-example.com to http://www.mt-example.com, and can be easily be changed to redirect to https://www.mt-example.com for secure websites.
Setup:
- FTP to your website and go into the web directory:
(ss) Shared Server: /var/www/html/
(gs) Grid-Service: /home/#####/domains/mt-example.com/html/
(dv) Dedicated-Virtual Server: /var/www/vhosts/mt-example.com/httpdocs/ - Create an .htaccess file with the following contents:
Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_HOST} ^mt-example.com RewriteRule ^(.*)$ http://www.mt-example.com/$1 [R=301]
Substituting your actual domain name for 'mt-example.com' (the backslash in front of the . in line 3 is necessary) Save this file.
Execution:
Open a browser and go to http://mt-example.com, you'll see that you are immediately redirected to http://www.mt-example.com.
How it works:
The first line turns on an important apache directive related to how mod_rewrite works and the second explicitly turns on mod_rewrite.
Line 3 tells mod_rewrite what to look for before invoking any rules. %{HTTP_HOST} means the requested domain name and ^mt-example.com means 'starts with mt-example.com' The backslash in front of the . in this line is necessary because the period symbol is a special character (see Example 1) In plain English this line reads "Any requests that start with mt-example.com satisfy this rule".
The last line is the part that tells mod_rewrite what to look for and what to do. 'RewriteRule' tells mod_rewrite to define a rule set and in this case is only invoked if the previous line containing "RewriteCond" is satisfied. It rewrites any request matching the previous line to http://www.mt-example.com, see Example 1 for details on regular expressions. The part that reads [R=301] lets search engines know that www. is the preferred prefix.
NOTE:
The line Options +FollowSymLinks is always required to make mod_rewrite work.
Revisions:
07-20-2009: Minor Fixes
Fields marked with an asterisk(*) are required. Comment on this article