Site Moved

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

Bin-Blog

mod_rewrite Directives - RewriteCond and RewriteRule

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

RewriteEngine

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

RewriteCond

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} ^http://www.google.com [NC]

The RewriteCond has two arguments plus an optional third one.

TestString

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...

%{VARIABLE_NAME}

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.

HTTP_USER_AGENT
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'
HTTP_REFERER
The URL of the page from where the current visitor came form.
REMOTE_ADDR
The IP of the visitor.
REMOTE_HOST
The visitor's host name.
SCRIPT_FILENAME
The filename of the page that was requested.
QUERY_STRING
The queries that was passed to the page.
REQUEST_URI
The called URI
REQUEST_FILENAME
The path of the file - ie. the URL of the page without the domain.

CondPattern

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.
<CondPattern
Compares the two string and return true if the TestString is lexically lower than the given pattern
>CondPattern
Return true if the TestString is lexically greater than the given pattern.
=CondPattern
Return true if the TestString and pattern are equal.
-d
Return true if TestString is a directory(folder)
-f
Return true if TestString is a file

Flags

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

RewriteRule

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.

Pattern

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.

Substitution

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.

Flags

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.

Eg.

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...

0 Comments: