Knowledgebase

How do I enable mod_rewrite on my account?

URL rewriting

mod_rewrite is used for rewriting a URL at the server level, giving the user output from a different location. For example a user may ask for http://www.somesite.com/widgets/blue/, but will really be given http://www.somesite.com/scripts/widget.php?colour=blue and the user will be none the wiser.

Some benefits of URL rewriting are:

  • Making website URLs more user and search engine friendly
  • Preventing undesired "inline linking" or "hot linking"
  • Not exposing the (web address-related) inner workings of a website to users

URL rewriting is not the same as redirection. For example if you want a user going to Address_A to get the page at Address_B,

  • Redirection: the browser asks for Address_A, and is told to go to Address_B
  • URL rewriting: the browser ends up at Address_B, but it thinks it is still at Address_A

You can define the way you want mod_rewrite to behave in an .htaccess file.

Rewrite engine

To make any of this work, you first need to ensure that mod_rewrite is switched on

RewriteEngine on

You only need to include RewriteEngine on once in an .htaccess file, and it may not be required at all if set in the server config file.

Rewrite rules

Any number of rules can be included, and they take the following form:

RewriteRule   []

A URL that matches will be rewritten as . The magic is that you can use regular expressions in the and references in the strings. For example:

RewriteRule /products/([0-9]+) /siteengine/products.php?id=$1

With this rule, can use following address: http://mydomain.com/products/123 and this will be rewritten to http://mydomain.com/siteengine/products.php?id=123

There's no way for your visitors to find out where your script resides (/siteengine in the example), what its name is (products.php), or the name of the parameter to pass (id)!


The optional parameter can contain one or more of the following (in square brackets, and comma separated)

  • [F] - Forbidden - the user will receive a 403 error for any URL or file that matches the rule
  • [L] - Last Rule - no more rules will be proccessed if this one was successful
  • [R][R=code] - Redirect - the user’s web browser will be visibly redirected to the substituted URL. If you use this flag you must prefix the substitution with http://www.somesite.com/, thus making it into a true URL. If no code is given, then a HTTP reponse of 302 (temporarily moved) is sent. You can also use [R=301] (permanently moved)
  • [NC] - No Case - regular expression will not be case sensitive (so instead of [a-zA-Z], you can use [a-z])
  • [QSA] - Query String Append - the 'query string' (stuff after the ?) will be passed from the original URL (the one we are rewriting) to the new URL

These are the most common flags - a full list of flags is given in the Apache mod_rewrite manual.

Rewrite conditions

You can also customise your rewriting using conditions. The format of a condition is:

RewriteCond  

Any rewrite condition affects the behaviour of the following RewriteRule.

It works like this: mod_rewrite takes all the RewriteRules and starts matching the current URL against each RewriteRule pattern. If there's a RewriteRule pattern that matches the URL, mod_rewrite checks if there are existing conditions for this RewriteRule, and if the first one returns true. If it does, the proper substitution will occur, but if not, mod_rewrite looks for remaining conditions. When there are no more conditions, the subsequent RewriteRule is checked.


Both the and can be simple strings or regular expressions.


The can contain HTTP header variables (eg HTTP_USER_AGENT, HTTP_REFERER), connection & request variables (eg REMOTE_ADDR, QUERY_STRING), server internal variables (eg DOCUMENT_ROOT, SERVER_NAME), system variables (TIME_YEAR, TIME_DAY) and others.


The can also include special additions like:

  • <, >, = simple comparison operators
  • -f if test_string is a file
  • -d if test_string is a directory

For example, the following will direct the user to a different page depending on their browser:

RewriteCond %{HTTP_USER_AGENT} ^Mozilla.*
RewriteRule ^/$ /homepage.max.html [L]

RewriteCond %{HTTP_USER_AGENT} ^Lynx.*
RewriteRule ^/$ /homepage.min.html [L]

RewriteRule ^/$ /homepage.std.html [L]

When a browser requests the index page, 3 things can happen:

  • browser with a Mozilla engine will be served homepage.max.html
  • using Lynx (text-based browser) the homepage.min.html will open
  • if the browser's name doesn't contain 'Mozilla' or 'Lynx', the standard homepage.std.html file will be sent

There are endless possibilities, including IP or time-dependent conditions.



Was this answer helpful?

Add to Favourites Add to Favourites

Print this Article Print this Article

Also Read
How to parse html as php (Views: 1891)

Powered by WHMCompleteSolution

Language: