Site Moved

This site has been moved to a new location - Bin-Blog. All new post will appear at the new location.


mod_rewrite Directives - RewriteCond and RewriteRule

In the last post on mod_rewrite, I used a basic example from to explain the working of the mod_rewrite module. Now I will try to explain the various options available in mod_rewrite.


This directive turns the rewrite engine on or off.
Syntax : RewriteEngine (on|off)
Example: RewriteEngine on

If you wish to disable rewriting, use RewriteEngine off instead of commenting out all the other lines. Since the rewrite engine is off by default, you have to turn it on every time you wish to use the mod_rewrite module. So the first line will be always...

RewriteEngine on


The 'if' statement of mod_rewrite. The commands given below a RewriteCond line will be evaluated only if this returns a true value.
Syntax : RewriteCond TestString ConditionPattern [Flags]
Example: RewriteCond %{HTTP_REFERER} ^ [NC]

The RewriteCond has two arguments plus an optional third one.


There are three main types of strings that can be used in TestString part of the directive.

RewriteRule Regular Expression Captures - $N

Here N is a number between 0 and 9. If you use the string $1 in the TestString part of the RewriteCond directive, it will be replaced by the first capture from the regular expression for the corresponding RewriteRule - ie. the RewriteRule that will follow this condition.

RewriteCond Regular Expression Captures - %N

For this string, N is a number between 1 and 9. If you use the $1 in the TestString part of the RewriteCond directive, it will be replaced by the first capture from the regular expression for the corresponding RewriteCond - ie. the RewriteCond above this condition.

Server Variables

This are given in the format...


The most commonly used Server variables are given below. To see the full list of available server variables, see the apache manual page on mod_rewrite.

A string containing the name of the browser the visitor is using. Eg 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8) Gecko/20051111 Firefox/1.5'
The URL of the page from where the current visitor came form.
The IP of the visitor.
The visitor's host name.
The filename of the page that was requested.
The queries that was passed to the page.
The called URI
The path of the file - ie. the URL of the page without the domain.


CondPattern is a regular expression - with some extra features...

You can prefix the '!' operator to any condition pattern to make RewriteCond return true if the pattern does NOT match.
Compares the two string and return true if the TestString is lexically lower than the given pattern
Return true if the TestString is lexically greater than the given pattern.
Return true if the TestString and pattern are equal.
Return true if TestString is a directory(folder)
Return true if TestString is a file


Flags are given inside squire brackets([FLAGS]). Two flags are available for RewriteCond...
No Case - the case is ignored in matching.
Use this to combine two consecutive RewriteCond with a logical OR - if this is not given, an AND condition is implied


Provides the rules for rewriting URLs.
Syntax : RewriteRule Pattern Substitution [Flags]
Example: RewriteRule ^/user/(.*) get_data.php?user=$1 [NC]

This is were the URL rewriting actually happens. This directive has two arguments plus an optional third one.


The pattern is a perl compactable regular expression used to match the current URL. You can capture this by using the parenthesis ie. '(' and ')'. All text between these will be captured and could be used in the Substitution part of RewriteRule.


The actual URL that is to be called is given here. This involves the path of the script and the parameters that should be given to it. This supports all the special features of TestString(like $N, %N and Server variables) besides plain text. It also supports the '!','<','>' and '=' prefix operators as we saw in the CondPattern.


Flats are the third argument to the RewriteRule directive. This should be given inside square brackets([FLAGS]). Flags is a comma-separated list of these flags...

R - Redirect
The URL in the address bar will change if this flag is used - as the server will use the HTTP response of 302 (MOVED TEMPORARILY) when redirecting the page.
F - Forbidden
Using this flag immediately sends a HTTP response of 403 (FORBIDDEN). Use this flag with appropriate RewriteConds to conditionally block some URLs - for example image hot linking from external sites.
G - Gone
Sends a HTTP response header of 410 (GONE). This flag is used to mark pages which no longer exist as gone.
L - Last
Stop the rewriting process in this rule and don't apply any more rules. Think Perl's last command or the break command of C.
NC - No Case
Makes the Pattern case-insensitive.
QSA - Query String Append
This flag will append the query string from the current URL to the substitution string.


RewriteRule ^/user/(.*?)/ get_data.php?user=$1 [NC,QSA]

Please keep in mind that this is just the most commonly used Flags - for the full list, go to the apache manual page on mod_rewrite.

Happy rewriting...