Apache HTTP Server Version 2.4
This document discusses the flags which are available to the
RewriteRule
directive,
providing detailed explanations and examples.
A RewriteRule
can have
its behavior modified by one or more flags. Flags are included in
square brackets at the end of the rule, and multiple flags are separated
by commas.
RewriteRule pattern target [Flag1,Flag2,Flag3]
Each flag (with a few exceptions) has a short form, such as
CO
, as well as a longer form, such as cookie
.
While it is most common to use
the short form, it is recommended that you familiarize yourself with the
long form, so that you remember what each flag is supposed to do.
Some flags take one or more arguments. Flags are not case sensitive.
Flags that alter metadata associated with the request (T=, H=, E=) have no affect in per-directory and htaccess context, when a substitution (other than '-') is performed during the same round of rewrite processing.
Presented here are each of the available flags, along with an example of how you might use them.
The [B] flag instructs RewriteRule
to escape non-alphanumeric
characters before applying the transformation.
mod_rewrite
has to unescape URLs before mapping them,
so backreferences are unescaped at the time they are applied.
Using the B flag, non-alphanumeric characters in backreferences
will be escaped. For example, consider the rule:
For similar escaping of server-variables, see the "escape" mapping-function
RewriteRule "^search/(.*)$" "/search.php?term=$1"
Given a search term of 'x & y/z', a browser will encode it as
'x%20%26%20y%2Fz', making the request 'search/x%20%26%20y%2Fz'. Without the B
flag, this rewrite rule will map to 'search.php?term=x & y/z', which
isn't a valid URL, and so would be encoded as
search.php?term=x%20&y%2Fz=
, which is not what was intended.
With the B flag set on this same rule, the parameters are re-encoded
before being passed on to the output URL, resulting in a correct mapping to
/search.php?term=x%20%26%20y%2Fz
.
RewriteRule "^search/(.*)$" "/search.php?term=$1" [B,PT]
Note that you may also need to set