Skip to main content

mod_rewrite, a beginner's guide (with examples)

Popularity Report

Total Popularity Score: 0

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Rank

Related Lists

Bookmark History

Saved by 23 people (7 private), first by anonymouse user on 2006-04-25


Public Sticky notes

onditional statements for RewriteRules.

Highlighted by djshiow

basic format for a RewriteCond is RewriteCond test_string cond_pattern.

Highlighted by djshiow

The .htaccess file is a text file which contains Apache directives. Any directives which you place in it will apply to the directory which the .htaccess file sits in, and any below it.

Highlighted by bobblebob

RewriteEngine on RewriteRule ^alice.html$ bob.html

Highlighted by bobblebob

RewriteRule > > P attern Substitution [OptionalFl >a >gs]

Highlighted by bobblebob

You can include backreferences and server variable names (%{VARNAME}) in the substitution. Backreferences to this RewriteRule should be written as $N, whereas backreferences to the previous RewriteCond should be written as %N.

A special substitution is -. This substitution tells Apache to not perform any substitution. I personally find that this is useful when using the F or G flags (see below), but there are other uses as well.

Highlighted by bobblebob

This is the only part of the RewriteRule which isn’t mandatory. Any flags which you use should be surrounded in square brackets, and comma separated. The flags which I find to be most useful are:

  • F - Forbidden. The user will receive a 403 error.

  • L - Last Rule. No more rules will be proccessed if this one was successful.

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

Highlighted by bobblebob