From: Rich Bowen
Introduction
The flags
B (escape backreferences)
C|chain
CO|cookie
DPI|discardpathinfo
E|env
F|forbidden
G|gone
H|handler
L|last
N|next
NC|nocase
NE|noescape
NS|nosubreq
P|proxy
PT|passthrough
QSA|qsappend
QSD|qsdiscard
R|redirect
S|skip
T|typecookie. Some flags take one or more
arguments. Flags are not case sensitive.
-Each flag (with a few exceptions) has a long and short form. While it is most common to use the short form, it is recommended that you familiarize yourself with the @@ -55,35 +70,93 @@ long form, so that you remember what each flag is supposed to do.
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.
Apache has to unescape URLs before mapping them, +so backreferences will be unescaped at the time they are applied. +Using the B flag, non-alphanumeric characters in backreferences +will be escaped. For example, consider the rule:
-
+RewriteRule ^(/.*)$ /index.php?show=$1
+
This will map /C++ to
+/index.php?show=/C++. But it will also map
+/C%2b%2b to /index.php?show=/C++, because
+the %2b has been unescaped. With the B flag, it will
+instead map to /index.php?show=/C%2b%2b.
This escaping is particularly necessary in a proxy situation, +when the backend may break if presented with an unescaped URL.
+ +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
skipped.
The [CO], or [cookie] flag, allows you to set a cookie when a
particular RewriteRule
-matches. The argument consists of three required fields and two optional
+matches. The argument consists of three required fields and five optional
fields.
You must declare a name and value for the cookie to be set, and the -domain for which you wish the cookie to be valid. You may optionally set -the lifetime of the cookie, and the path for which it should be -returned.
-By default, the lifetime of the cookie is the current browser -session.
-By default, the path for which the cookie will be valid is "/" - that -is, the entire website.
+ +The full syntax for the flag, including all attributes, is as +follows:
+ +
+[CO=NAME:VALUE:domain:lifetime:path:secure:httponly]
+
You must declare a name and value for the cookie to be set.
+ +You may optionally also set the following values:
+ +www.example.com, or it may be a domain,
+such as .example.com. It must be at least two parts
+separated by a dot. That is, it may not be merely .com or
+.net. Cookies of that kind are forbidden by the cookie
+security model./customers/ or /files/download/./ - that is, the entire
+website.secure, true, or 1,
+the cookie will only be permitted to be translated via secure (https)
+connections.HttpOnly, true, or
+1, the cookie will have the HttpOnly flag set,
+which means that the cookie will be inaccessible to JavaScript code on
+browsers that support this feature.Several examples are offered here:
@@ -98,20 +171,58 @@ called 'frontdoor' to a value of 'yes'. The cookie is valid for any host
in the .apache.org domain. It will be set to expire in 1440
minutes (24 hours) and will be returned for all URIs.
The DPI flag causes the PATH_INFO portion of the rewritten URI to be discarded.
- - -This flag is available from 2.2.12
+In per-directory context, the URI each RewriteRule
+compares against is the concatenation of the current values of the URI
+and PATH_INFO.
The current URI can be the initial URI as requested by the client, the +result of a previous round of mod_rewrite processing, or the result of +a prior rule in the current round of mod_rewrite processing.
+ +In contrast, the PATH_INFO that is appended to the URI before each
+rule reflects only the value of PATH_INFO before this round of
+mod_rewrite processing. As a consequence, if large portions
+of the URI are matched and copied into a substitution in multiple
+RewriteRule directives, without regard for
+which parts of the URI came from the current PATH_INFO, the final
+URI may have multiple copies of PATH_INFO appended to it.
Use this flag on any substitution where the PATH_INFO that resulted +from the previous mapping of this request to the filesystem is not of +interest. This flag permanently forgets the PATH_INFO established +before this round of mod_rewrite processing began. PATH_INFO will +not be recalculated until the current round of mod_rewrite processing +completes. Subsequent rules during this round of processing will see +only the direct result of substitutions, without any PATH_INFO +appended.
+With the [E], or [env] flag, you can set the value of an environment variable. Note that some environment variables may be set after the rule is run, thus unsetting what you have set. See the Environment Variables document for more details on how Environment variables work.
+The syntax for this flag is:
+ +
+[E:VAR=VAL]
+
VAL may contain backreferences ($N or
+%N) which will be expanded.
These environment variables can then be used in a variety of +contexts, including CGI programs, other RewriteRule directives, or +CustomLog directives.
+The following example sets an evironment variable called 'image' to a value of '1' if the requested URI is an image file. Then, that environment variable is used to exclude those requests from the access @@ -124,9 +235,9 @@ CustomLog logs/access_log combined env=!image
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
@@ -143,9 +254,9 @@ RewriteRule \.exe - [F]
that the requested URI is not modified. There's no reason to rewrite to
another URI, if you're going to forbid the request.
The [G] flag forces Apache to return a 410 Gone status with the response. This indicates that a resource used to be available, but is no longer available.
@@ -156,9 +267,9 @@ rewrite target when using the [G] flag:
RewriteRule oldproduct - [G,NC]
Forces the resulting request to be handled with the specified handler. For example, one might use this to force all files without a file extension to be parsed by the php handler:
@@ -186,9 +297,9 @@ match any request that starts with/source/ followed by 1 or
n characters followed by .phps literally. The backreference
$1 referrers to the captured match within parenthesis of the regular
expression.
-
-
-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.
index.php, the N|next
+
The [N] flag causes the ruleset to start over again from the top. Use
with extreme caution, as it may result in loop.
@@ -244,9 +355,9 @@ pattern still matches (i.e., while the URI still contains an
A), perform this substitution (i.e., replace the
A with a B).
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.
RewriteRule (.*\.(jpg|gif|png))$ http://images.example.com$1 [P,NC]
By default, special characters, such as & and
?, for example, will be converted to their hexcode
equivalent. Using the [NE] flag prevents that from happening.
@@ -278,9 +389,9 @@ being converted to its hexcode equivalent, %23, which will
then result in a 404 Not Found error condition.
Use of the [NS] flag prevents the rule from being used on subrequests. For example, a page which is included using an SSI (Server Side Include) is a subrequest, and you may want to avoid rewrites @@ -291,9 +402,9 @@ Images, javascript files, or css files, loaded as part of an HTML page, are not subrequests - the browser requests them as separate HTTP requests.
- - -Use of the [P] flag causes the request to be handled by
mod_proxy, and handled via a proxy request. For
example, if you wanted all image requests to be handled by a back-end
@@ -307,9 +418,9 @@ RewriteRule (.*)\.(jpg|gif|png) http://images.example.com$1.$2 [P]
pushed through the proxy, and any following rules will not be
considered.
The target (or substitution string) in a RewriteRule is assumed to be a @@ -338,9 +449,9 @@ Omission of the [PT] flag in this case will cause the Alias to be ignored, resulting in a 'File not found' error being returned.
- - -
When the replacement URI contains a query string, the default behavior
of RewriteRule is to discard
@@ -360,9 +471,9 @@ flag, that same request will be mapped to
/page.php?page=123 - that is, the existing query string
will be discarded.
When the requested URI contains a query string, and the target URI does
not, the default behavior of RewriteRule is to copy that query
@@ -381,9 +492,9 @@ replaced with the query string in the RewriteRule target
URI.
Use of the [R] flag causes a HTTP redirect to be issued to the browser. If a fully-qualified URL is specified (that is, including @@ -405,9 +516,9 @@ on to the next rule in the ruleset, which can often result in 'Invalid URI in request' warnings.
- - -The [S] flag is used to skip rules that you don't want to run. This
can be thought of as a goto statement in your rewrite
ruleset. In the following example, we only want to run the RewriteRule if the requested URI
@@ -430,9 +541,9 @@ following it. Thus, if you want to make a RewriteCond apply
to several RewriteRules, 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.
<FilesMat
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 |
diff --git a/docs/manual/rewrite/flags.xml b/docs/manual/rewrite/flags.xml
index 98e01939601..e1c904f016b 100644
--- a/docs/manual/rewrite/flags.xml
+++ b/docs/manual/rewrite/flags.xml
@@ -47,10 +47,6 @@ RewriteRule pattern target [Flag1,Flag2,Flag3]
a longer form, such as cookie. Some flags take one or more
arguments. Flags are not case sensitive.
Each flag (with a few exceptions) has a long and short form. While it is most common to use the short form, it is recommended that you familiarize yourself with the @@ -58,12 +54,32 @@ long form, so that you remember what each flag is supposed to do.
Presented here are each of the available flags, along with an example of how you might use them.
+The [B] flag instructs
Apache has to unescape URLs before mapping them, +so backreferences will be unescaped at the time they are applied. +Using the B flag, non-alphanumeric characters in backreferences +will be escaped. For example, consider the rule:
+ +This will map /C++ to
+/index.php?show=/C++. But it will also map
+/C%2b%2b to /index.php?show=/C++, because
+the %2b has been unescaped. With the B flag, it will
+instead map to /index.php?show=/C%2b%2b.
This escaping is particularly necessary in a proxy situation, +when the backend may break if presented with an unescaped URL.
+The [CO], or [cookie] flag, allows you to set a cookie when a
particular
You must declare a name and value for the cookie to be set, and the -domain for which you wish the cookie to be valid. You may optionally set -the lifetime of the cookie, and the path for which it should be -returned.
-By default, the lifetime of the cookie is the current browser -session.
-By default, the path for which the cookie will be valid is "/" - that -is, the entire website.
+ +The full syntax for the flag, including all attributes, is as +follows:
+ +You must declare a name and value for the cookie to be set.
+ +You may optionally also set the following values:
+ +www.example.com, or it may be a domain,
+such as .example.com. It must be at least two parts
+separated by a dot. That is, it may not be merely .com or
+.net. Cookies of that kind are forbidden by the cookie
+security model./customers/ or /files/download/./ - that is, the entire
+website.secure, true, or 1,
+the cookie will only be permitted to be translated via secure (https)
+connections.HttpOnly, true, or
+1, the cookie will have the HttpOnly flag set,
+which means that the cookie will be inaccessible to JavaScript code on
+browsers that support this feature.Several examples are offered here:
The DPI flag causes the PATH_INFO portion of the rewritten URI to be discarded.
+This flag is available from 2.2.12
+In per-directory context, the URI each
The current URI can be the initial URI as requested by the client, the +result of a previous round of mod_rewrite processing, or the result of +a prior rule in the current round of mod_rewrite processing.
+ +In contrast, the PATH_INFO that is appended to the URI before each
+rule reflects only the value of PATH_INFO before this round of
+mod_rewrite processing. As a consequence, if large portions
+of the URI are matched and copied into a substitution in multiple
+
Use this flag on any substitution where the PATH_INFO that resulted +from the previous mapping of this request to the filesystem is not of +interest. This flag permanently forgets the PATH_INFO established +before this round of mod_rewrite processing began. PATH_INFO will +not be recalculated until the current round of mod_rewrite processing +completes. Subsequent rules during this round of processing will see +only the direct result of substitutions, without any PATH_INFO +appended.
The syntax for this flag is:
+ +VAL may contain backreferences ($N or
+%N) which will be expanded.
These environment variables can then be used in a variety of +contexts, including CGI programs, other RewriteRule directives, or +CustomLog directives.
+The following example sets an evironment variable called 'image' to a value of '1' if the requested URI is an image file. Then, that environment variable is used to exclude those requests from the access @@ -478,6 +570,5 @@ solutions to a problem before resorting to rewrite, which will invariably be a less efficient solution than the alternatives.