This document supplements the
The Apache module
This document attempts to give sufficient background so that what follows is understood, rather than just copied blindly.
Remember that many common URL-manipulation tasks don't require the
full power and complexity of
Finally, before proceeding, be sure to configure
mod_rewrite uses the Perl Compatible Regular Expression vocabulary. In this document, we do not attempt to provide a detailed reference to regular expressions. For that, we recommend the PCRE man pages, the Perl regular expression man page, and Mastering Regular Expressions, by Jeffrey Friedl.
In this document, we attempt to provide enough of a regex vocabulary
to get you started, without being overwhelming, in the hope that
The following are the minimal building blocks you will need, in order
to write regular expressions and
Character | Meaning | Example |
---|---|---|
. | Matches any single character | c.t will match cat ,
cot , cut , etc. |
+ | Repeats the previous match one or more times | a+ matches a , aa ,
aaa , etc |
* | Repeats the previous match zero or more times. | a* matches all the same things
a+ matches, but will also match an empty string. |
? | Makes the match optional. |
colou?r will match color and colour . |
^ | Called an anchor, matches the beginning of the string | ^a matches a string that begins with
a |
$ | The other anchor, this matches the end of the string. | a$ matches a string that ends with
a . |
( ) | Groups several characters into a single unit, and captures a match for use in a backreference. | (ab)+
matches ababab - that is, the + applies to the group.
For more on backreferences see below. |
[ ] | A character class - matches one of the characters | c[uoa]t matches cut ,
cot or cat . |
[^ ] | Negative character class - matches any character not specified | c[^/]t matches cat or c=t but not c/t |
In !
character can be
used before a regular expression to negate it. This is, a string will
be considered to have matched only if it does not match the rest of
the expression.
One important thing here has to be remembered: Whenever you
use parentheses in Pattern or in one of the
CondPattern, back-references are internally created
which can be used with the strings $N
and
%N
(see below). These are available for creating
the Substitution parameter of a
Captures in the
Figure 1 shows to which locations the back-references are transferred for expansion as well as illustrating the flow of the RewriteRule, RewriteCond matching. In the next chapters, we will be exploring how to use these back-references, so do not fret if it seems a bit alien to you at first.
Figure 1: The back-reference flow through a rule.
In this example, a request for /test/1234
would be transformed into /admin.foo?page=test&id=1234&host=admin.example.com
.
A
The Pattern is a regular expression. It is initially (for the first rewrite rule or until a substitution occurs) matched against the URL-path of the incoming request (the part after the hostname but before any question mark indicating the beginning of a query string) or, in per-directory context, against the request's path relative to the directory for which the rule is defined. Once a substitution has occurred, the rules that follow are matched against the substituted value.
Figure 2: Syntax of the RewriteRule directive.
The Substitution can itself be one of three things:
This maps a request to an arbitrary location on your filesystem, much
like the
If /usr/local/apache2/htdocs
, then this directive would
map requests for http://example.com/foo
to the
path /usr/local/apache2/htdocs/bar
.
This tells the client to make a new request for the specified URL.
The Substitution can also contain back-references to parts of the incoming URL-path matched by the Pattern. Consider the following:
The variable $1
will be replaced with whatever text
was matched by the expression inside the parenthesis in
the Pattern. For example, a request
for http://example.com/product/r14df/view
will be mapped
to the path /var/web/productdb/r14df
.
If there is more than one expression in parenthesis, they are
available in order in the
variables $1
, $2
, $3
, and so
on.
The behavior of a [NC]
flag:
For more details on the available flags, their meanings, and examples, see the Rewrite Flags document.
One or more
Figure 3: Syntax of the RewriteCond directive
For example, to send all requests from a particular IP range to a different server, you could use:
When more than
one
Notice that the exclamation mark specifies a negative match, so the rule is only applied if the cookie does not contain "go".
Matches in the regular expressions contained in
the %1
, %2
, etc. For example, this
will direct the request to a different directory depending on the
hostname used to access the site:
If the request was for http://example.com/foo/bar
,
then %1
would contain example.com
and $1
would contain foo/bar
.
The
Rewriting is typically configured in the main server configuration
setting (outside any .htaccess
files at the expense of some additional complexity. This technique
is called per-directory rewrites.
The main difference with per-server rewrites is that the path
prefix of the directory containing the .htaccess
file is
stripped before matching in
the