From 17bcbb701afbb28d0616e4f6cbd65b67fc77b406 Mon Sep 17 00:00:00 2001
From: Joshua Slive
Apache HTTP Server Version 2.3
-Available Languages: en
+Available Languages:
This document discusses the flags which are available to the
-RewriteRule
directive,
-providing more detailed explanations and examples of each.
RewriteRule
directive,
+providing detailed explanations and examples. This is not necessarily
+a comprehensive list of all flags available, so be sure to also
+consult the reference documentation.
RewriteRule
s can have
+
RewriteRule
s can have
their 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.
cookie
. Some flags take one or more
arguments. Flags are not case sensitive.
-The [C] or [chain] flag indicates that the RewriteRule
is chained to the next
+
The [C] or [chain] flag indicates that the RewriteRule
is chained to the next
rule. That is, if the rule matches, then it is processed as usual and
control moves on to the next rule. However, if it does not match, then
the next rule, and any other rules that are chained together, will be
@@ -66,7 +68,7 @@ skipped.
The [CO], or [cookie] flag, allows you to set a cookie when a
-particular RewriteRule
+particular RewriteRule
matches. The argument consists of three required fields and two optional
fields.
You must declare a name and value for the cookie to be set, and the
@@ -109,14 +111,14 @@ RewriteRule \.(png|gif|jpg) - [E=image:1]
CustomLog logs/access_log combined env=!image
Note that this same effect can be obtained using SetEnvIf
. This technique is offered as
+
Note that this same effect can be obtained using SetEnvIf
. This technique is offered as
an example, not as a recommendation.
Using the [F] flag causes Apache to return a 403 Forbidden status
code to the client. While the same behavior can be accomplished using
-the Deny
directive, this
+the Deny
directive, this
allows more flexibility in assigning a Forbidden status.
The following rule will forbid .exe
files from being
@@ -161,25 +163,25 @@ that does not contain the literal .
character.
The [L] flag causes mod_rewrite
to stop processing
+
The [L] flag causes mod_rewrite
to stop processing
the rule set. In most contexts, this means that if the rule matches, no
further rules will be processed.
If you are using RewriteRule
in either
+
If you are using RewriteRule
in either
.htaccess
files or in
-<Directory>
sections,
+<Directory>
sections,
it is important to have some understanding of how the rules are
processed. The simplified form of this is that once the rules have been
processed, the rewritten request is handed back to the URL parsing
engine to do what it may with it. It is possible that as the rewritten
request is handled, the .htaccess
file or
-<Directory>
section
+<Directory>
section
may be encountered again, and thus the ruleset may be run again from the
start. Most commonly this will happen if one of the rules causes a
redirect - either internal or external - causing the request process to
start over.
It is therefore important, if you are using RewriteRule
directives in one of these
+
It is therefore important, if you are using RewriteRule
directives in one of these
context that you take explicit steps to avoid rules looping, and not
count solely on the [L] flag to terminate execution of a series of
rules, as shown below.
Use of the [NC] flag causes the RewriteRule
to be matched in a
+
Use of the [NC] flag causes the RewriteRule
to be matched in a
case-insensitive manner. That is, it doesn't care whether letters appear
as upper-case or lower-case in the matched URI.
Use of the [P] flag causes the request to be handled by
-mod_proxy
, and handled via a proxy request. For
+mod_proxy
, and handled via a proxy request. For
example, if you wanted all image requests to be handled by a back-end
image server, you might do something like the following:
RewriteRule
to be passed back through
-URL mapping, so that location-based mappings, such as Alias
, for example, might have a chance to take
+use of the [PT] flag causes the result of the RewriteRule
to be passed back through
+URL mapping, so that location-based mappings, such as Alias
, for example, might have a chance to take
effect.
If, for example, you have an
-Alias
-for /icons, and have a RewriteRule
pointing there, you should
+Alias
+for /icons, and have a RewriteRule
pointing there, you should
use the [PT] flag to ensure that the
-Alias
is evaluated.
+Alias
is evaluated.
When the replacement URI contains a query string, the default behavior
-of The [S] flag is used to skip rules that you don't want to run. This
can be thought of as a This technique is useful because a This technique is useful because a
@@ -313,7 +315,7 @@ ignored, resulting in a 'File not found' error being returned.
QSA|qsappend
RewriteRule
is to discard
+of RewriteRule
is to discard
the existing query string, and replace it with the newly generated one.
Using the [QSA] flag causes the query strings to be combined.
S|skip
goto
statement in your rewrite
-ruleset. In the following example, we only want to run the RewriteRule
if the requested URI
+ruleset. In the following example, we only want to run the RewriteRule
if the requested URI
doesn't correspond with an actual file.
@@ -373,8 +375,8 @@ RewriteRule (.*\.gif) images.php?$1
RewriteRule (.*\.html) docs.php?$1
RewriteCond
only applies to the
-RewriteRule
immediately
+RewriteCond
only applies to the
+RewriteRule
immediately
following it. Thus, if you want to make a RewriteCond
apply
to several RewriteRule
s, one possible technique is to
negate those conditions and use a [Skip] flag.
Sets the MIME type with which the resulting response will be
-sent. This has the same effect as the AddType
directive.
AddType
directive.
For example, you might use the following technique to serve Perl source code as plain text, if requested in a particular way:
@@ -403,7 +405,7 @@ RewriteRule IMG - [T=image/jpg]Please note that this is a trivial example, and could be better done
-using <FilesMatch>
+using <FilesMatch>
instead. Always consider the alternate
solutions to a problem before resorting to rewrite, which will
invariably be a less efficient solution than the alternatives.
Available Languages: en
+Available Languages: