RewriteRule [ pattern substitution]
Default
None
Context
server config, <Global>, <VirtualHost>, <Anonymous>, <Directory>
Module
mod_rewrite
Compatibility
1.2.6rc1 and later
The RewriteRule directive is the real rewriting workhorse. The configuration directive can occur more than once. Each directive defines a single rewriting rule. The order of definition of these rules is important, because this order is used when applying the rules at run-time.
Pattern can be POSIX regular expression which gets applied to the current FTP command argument(s).
Some hints about the syntax of regular expressions:
Text:
. Any single character [chars] Character class: one of chars [^chars] Character class: none of chars text1|text2 Alternative: text1 or text2
Quantifiers:
? 0 or 1 of the preceding text * 0 or N of the preceding text (N > 0) + 1 or N of the preceding text (N > 1)
Grouping:
(text) Grouping of text
(either to set the borders of an alternative or
for making backreferences where the Nth group can
be used on the RHS of a RewriteRule with $N)
Anchors:
^ Start of line anchor $ End of line anchor
Escaping:
\char Escape that particular char
(for instance to specify the chars ".[]()" etc.)
For more information about regular expressions have a look at your local regex(3) manpage. If you are interested in more detailed information about regular expressions and their variants (POSIX regex, Perl regex, etc.) have a look at the following dedicated book on this topic:
Mastering Regular Expressions Jeffrey E.F. Friedl Nutshell Handbook Series O'Reilly & Associates, Inc. 1997 ISBN 1-56592-257-3
Additionally in mod_rewrite the NOT character ('!') is a possible pattern prefix. This gives you the ability to negate a pattern; to say, for instance: "if the current argument(s) does NOT match this pattern". This can be used for exceptional cases, where it is easier to match the negative pattern, or as a last default rule.
Notice: When using the NOT character to negate a pattern you cannot have grouped wildcard parts in the pattern. This is impossible because when the pattern does NOT match, there are no contents for the groups. In consequence, if negated patterns are used, you cannot use $N in the substitution string.
Substitution of a rewriting rule is the string which is substituted for (or replaces) the original argument(s) for which pattern matched. Beside plain text you can use:
$N backreferences to the RewriteRule pattern
%N backreferences to the last matched RewriteCondition pattern
variables as in RewriteCondition test strings
map function calls (${map-name:lookup-key|default-value})
Backreferences are $N (N=0..9) identifiers which will be replaced by the contents of the Nth group of the matched pattern. The variables are the same as for the condition of a RewriteCondition directive, with two additions:
%P process ID
%t Unix time since the epoch, in seconds
The map functions come from the RewriteMap directive and are explained there. These four types of variables are expanded in the order of the above list.
All of the rewriting rules are applied to substitution. The command argument(s) is completely replaced by the substitution.