Backreferences are only defined if the pattern
+ matches. Thus, if the pattern is prefixed with
+ !, no backreferences are ever defined.
+
+
+
+
@@ -299,7 +308,7 @@ URLs on the fly
correspond to the similarly named HTTP
MIME-headers, C variables of the Apache HTTP Server or
struct tm fields of the Unix system.
- Most are documented in the
+ Most are documented in the
Expressions doc, in the
Environment Variables doc,
or the CGI specification (RFC 3875).
@@ -309,7 +318,30 @@ URLs on the fly
UseCanonicalPhysicalPort
respectively.
- Those that are special to mod_rewrite include those below.
+ The variables SCRIPT_FILENAME and REQUEST_FILENAME
+ contain the same value - the value of the
+ filename field of the internal
+ request_rec structure of the Apache HTTP Server.
+ The first name is the commonly known CGI variable name
+ while the second is the appropriate counterpart of
+ REQUEST_URI (which contains the value of the
+ uri field of request_rec).
+
+ If a substitution occurred and the rewriting continues,
+ the value of both variables will be updated accordingly.
+
+ If used in per-server context (i.e., before the
+ request is mapped to the filesystem) SCRIPT_FILENAME and
+ REQUEST_FILENAME cannot contain the full local filesystem
+ path since the path is unknown at this stage of processing.
+ Both variables will initially contain the value of REQUEST_URI
+ in that case. In order to obtain the full local filesystem
+ path of the request in per-server context, use an URL-based
+ look-ahead %{LA-U:REQUEST_FILENAME} to determine
+ the final value of REQUEST_FILENAME.
+
+ Those that are special to mod_rewrite
+ include those below.
API_VERSION
@@ -372,7 +404,7 @@ URLs on the fly
such as "/index.html". This notably excludes the
query string which is available as its own variable
named QUERY_STRING. The value returned for
- REQUEST_URI
+ REQUEST_URI
has already been %-decoded, to re-encode it pass it through
the "escape" mapping-function.
Note that this server variable differs from the CGI
@@ -393,79 +425,63 @@ URLs on the fly
(decoded), unlike most other variables below.
-
-
- If the TestString has the special value expr,
- the CondPattern will be treated as an
- ap_expr. HTTP headers referenced in the
- expression will be added to the Vary header if the novary
- flag is not given.
+
- Other things you should be aware of:
+ Prefixed Variable Lookups
-
- -
-
The variables SCRIPT_FILENAME and REQUEST_FILENAME
- contain the same value - the value of the
- filename field of the internal
- request_rec structure of the Apache HTTP Server.
- The first name is the commonly known CGI variable name
- while the second is the appropriate counterpart of
- REQUEST_URI (which contains the value of the
- uri field of request_rec).
- If a substitution occurred and the rewriting continues,
- the value of both variables will be updated accordingly.
- If used in per-server context (i.e., before the
- request is mapped to the filesystem) SCRIPT_FILENAME and
- REQUEST_FILENAME cannot contain the full local filesystem
- path since the path is unknown at this stage of processing.
- Both variables will initially contain the value of REQUEST_URI
- in that case. In order to obtain the full local filesystem
- path of the request in per-server context, use an URL-based
- look-ahead %{LA-U:REQUEST_FILENAME} to determine
- the final value of REQUEST_FILENAME.
-
- -
-
%{ENV:variable}, where variable can be
- any environment variable, is also available.
- This is looked-up via internal
+ In addition to the server variables above, the
+ %{PREFIX:name} syntax provides
+ access to additional sources:
+
+
+ %{ENV:variable}
+ - Where variable can be
+ any environment variable. This is looked-up via internal
Apache httpd structures and (if not found there) via
-
getenv() from the Apache httpd server process.
+ getenv() from the Apache httpd server process.
- -
-
%{SSL:variable}, where variable is the
+ %{SSL:variable}
+ - Where variable is the
name of an SSL environment
- variable, can be used whether or not
+ variable. This can be used whether or not
mod_ssl is loaded, but will always expand to
the empty string if it is not. Example:
%{SSL:SSL_CIPHER_USEKEYSIZE} may expand to
128. These variables are available even without
setting the StdEnvVars option of the
- SSLOptions directive.
+ SSLOptions directive.
- -
-
%{HTTP:header}, where header can be
- any HTTP MIME-header name, can always be used to obtain the
+ %{HTTP:header}
+ - Where header can be
+ any HTTP MIME-header name. This can always be used to obtain the
value of a header sent in the HTTP request.
Example:
%{HTTP:Proxy-Connection} is
the value of the HTTP header
``Proxy-Connection:''.
- If a HTTP header is used in a condition this header is added to
+
If an HTTP header is used in a condition, this header is added to
the Vary header of the response in case the condition evaluates
to true for the request. It is not added if the
condition evaluates to false for the request. Adding the HTTP header
to the Vary header of the response is needed for proper caching.
It has to be kept in mind that conditions follow a short circuit
logic in the case of the 'ornext|OR' flag
- so that certain conditions might not be evaluated at all.
+ so that certain conditions might not be evaluated at all.
+
+
+
- -
-
%{LA-U:variable}
- can be used for look-aheads which perform
- an internal (URL-based) sub-request to determine the final
+ Look-ahead Sub-requests
+
+ These forms perform an internal sub-request to determine the
+ final value of a variable that is not yet available at the current
+ stage of processing:
+
+
+ %{LA-U:variable}
+ - Performs an internal (URL-based) sub-request to determine the final
value of variable. This can be used to access
- variable for rewriting which is not available at the current
+ a variable for rewriting which is not available at the current
stage, but will be set in a later phase.
For instance, to rewrite according to the
REMOTE_USER variable from within the
@@ -478,14 +494,29 @@ URLs on the fly
its per-directory context via
the Fixup phase of the API and because the authorization
phases come before this phase, you just can use
- %{REMOTE_USER} in that context.
+ %{REMOTE_USER} in that context.
- -
-
%{LA-F:variable} can be used to perform an internal
- (filename-based) sub-request, to determine the final value
- of variable. Most of the time, this is the same as
- LA-U above.
-
+ %{LA-F:variable}
+ Performs an internal (filename-based) sub-request to determine
+ the final value of variable. Most of the time, this is
+ the same as LA-U above.
+
+
+
+
+ Expression Syntax
+
+ If the TestString has the special value expr,
+ the CondPattern will be treated as an
+ ap_expr. HTTP headers referenced in the
+ expression will be added to the Vary header if the novary
+ flag is not given.
+
+
+
+
+
+ CondPattern
CondPattern is the condition pattern,
a regular expression which is applied to the
@@ -498,16 +529,18 @@ URLs on the fly
additional syntax available to perform other useful tests against
the Teststring:
-
- - You can prefix the pattern string with a
+
+
+
You can prefix the pattern string with a
'!' character (exclamation mark) to negate the result
of the condition, no matter what kind of CondPattern is used.
-
+
+
+
- -
- You can perform lexicographical string comparisons:
+
String Comparisons
-
+
- <CondPattern
- Lexicographically precedes
Treats the CondPattern as a plain string and
@@ -547,21 +580,21 @@ URLs on the fly
if TestString lexicographically follows
CondPattern, or is equal to CondPattern
(the two strings are equal, character for character).
-
+
+
Note
The string comparison operator is part of the
CondPattern
argument and must be included in the quotes if those are used. Eg.
-
+
RewriteCond %{HTTP_USER_AGENT} "=This Robot/1.0"
-
-
- -
- You can perform integer comparisons:
-
+
+
+ Integer Comparisons
+
- -eq
- Is numerically equal to
The TestString is treated as an integer, and is
@@ -606,32 +639,26 @@ URLs on the fly
numerically compared to the CondPattern. True if
the two are numerically different. This is equivalent to
!-eq.
+
-
-
-
- - You can perform various file attribute tests:
-
+
-
+ File Attribute Tests
+
- -d
-
- Is directory.
Treats the TestString as a pathname and tests
whether or not it exists, and is a directory.
- -f
-
- Is regular file.
-
Treats the TestString as a pathname and tests
whether or not it exists, and is a regular file.
-
+
- -F
-
- Is existing file, via subrequest.
Checks whether or not TestString is a valid file,
accessible via all the server's currently-configured
@@ -646,7 +673,6 @@ URLs on the fly
- -l
-
- Is symbolic link.
Treats the TestString as a pathname and tests
whether or not it exists, and is a symbolic link. May also
@@ -685,18 +711,18 @@ URLs on the fly
whether or not it exists, and has executable permissions.
These permissions are determined according to
the underlying OS.
+
-
-
- For example:
+ For example:
RewriteCond /var/www/%{REQUEST_URI} !-f
RewriteRule ^(.+) /other/archive/$1 [R]
-
+
+
+ Expression Evaluation
- -
If the TestString has the special value expr, the
CondPattern will be treated as an
ap_expr.
@@ -710,28 +736,31 @@ RewriteRule ^(.+) /other/archive/$1 [R]
RewriteCond expr "! %{HTTP_REFERER} -strmatch '*://%{HTTP_HOST}/*'"
RewriteRule "^/images" "-" [F]
-
-
- You can also set special flags for CondPattern by appending
+
+
+
+
+
Flags
+
+ You can set special flags for CondPattern by appending
[flags]
as the third argument to the RewriteCond
directive, where flags is a comma-separated list of any of the
following flags:
-
-
- - '
nocase|NC'
- (no case)
+
+
+ - '
nocase|NC'
+ - (no case)
This makes the test case-insensitive - differences
between 'A-Z' and 'a-z' are ignored, both in the
expanded TestString and the CondPattern.
This flag is effective only for comparisons between
TestString and CondPattern. It has no
- effect on filesystem and subrequest checks.
+ effect on filesystem and subrequest checks.
- -
- '
ornext|OR'
- (or next condition)
+ - '
ornext|OR'
+ - (or next condition)
Use this to combine rule conditions with a local OR
instead of the implicit AND. Typical example:
@@ -743,20 +772,24 @@ RewriteRule ...some special stuff for any of these hosts...
Without this flag you would have to write the condition/rule
pair three times.
-
+
- - '
novary|NV'
- (no vary)
- If a HTTP header is used in the condition, this flag prevents
+ - '
novary|NV'
+ - (no vary)
+ If an HTTP header is used in the condition, this flag prevents
this header from being added to the Vary header of the response.
Using this flag might break proper caching of the response if
the representation of this response varies on the value of this header.
So this flag should be only used if the meaning of the Vary header
is well understood.
-
-
+
+
+
+
+
+
- Example:
+ Example
To rewrite the Homepage of a site according to the
``User-Agent:'' header of the request, you can
diff --git a/docs/manual/mod/mod_rewrite.xml b/docs/manual/mod/mod_rewrite.xml
index ffcab8e97f..0830e2e0c4 100644
--- a/docs/manual/mod/mod_rewrite.xml
+++ b/docs/manual/mod/mod_rewrite.xml
@@ -470,50 +470,66 @@ RewriteRule "^/ex/(.*)" "${examplemap:$1}"
condition is determined to be true only if the the
CondPattern does not match.
- TestString is a string which can contain the
+
+
+
+
+
TestString
+
+ TestString is a string which can contain the
following expanded constructs in addition to plain text:
-
- -
- RewriteRule backreferences: These are
- backreferences of the form
$N
+
+
+ Backreferences
+
+
+ $N — RewriteRule backreferences
+ - Backreferences of the form
$N
(0 <= N <= 9). $1 to $9 provide access to the grouped
parts (in parentheses) of the pattern, from the
RewriteRule which is subject to the current
set of RewriteCond conditions. $0 provides
- access to the whole string matched by that pattern.
-
- Backreferences are only defined if the pattern
- matches. Thus, if the pattern is prefixed with
- !, no backreferences are ever defined.
-
- -
- RewriteCond backreferences: These are
- backreferences of the form
%N
+ access to the whole string matched by that pattern.
+
+ %N — RewriteCond backreferences
+ - Backreferences of the form
%N
(0 <= N <= 9). %1 to %9 provide access to the grouped
parts (again, in parentheses) of the pattern, from the last matched
RewriteCond in the current set
of conditions. %0 provides access to the whole string matched by
- that pattern.
-
- Backreferences are only defined if the pattern
- matches. Thus, if the pattern is prefixed with
- !, no backreferences are ever defined.
-
- -
- RewriteMap expansions: These are
- expansions of the form
${mapname:key|default}.
- See the documentation for
- RewriteMap for more details.
-
- -
- Server variables: These are variables of
- the form
-
%{ NAME_OF_VARIABLE
- }
- where NAME_OF_VARIABLE can be a string taken
- from the following list:
+ that pattern.
+
+
+ Backreferences are only defined if the pattern
+ matches. Thus, if the pattern is prefixed with
+ !, no backreferences are ever defined.
+ See How the
+ Ruleset is Applied for more details on the order in which
+ patterns are matched and backreferences populated.
+
+
+
+ RewriteMap Expansions
+
+ These are expansions of the form ${mapname:key|default}.
+ See the documentation for
+ RewriteMap for more details.
+
+
+
+ Server and CGI Variables
+
+ These are variables of the form
+ %{ NAME_OF_VARIABLE
+ }
+ where NAME_OF_VARIABLE can be a string taken
+ from the following list:
@@ -599,7 +615,7 @@ RewriteRule "^/ex/(.*)" "${examplemap:$1}"
correspond to the similarly named HTTP
MIME-headers, C variables of the Apache HTTP Server or
struct tm fields of the Unix system.
- Most are documented in the
+ Most are documented in the
Expressions doc, in the
Environment Variables doc,
or the CGI specification (3875).
@@ -609,7 +625,30 @@ RewriteRule "^/ex/(.*)" "${examplemap:$1}"
UseCanonicalPhysicalPort
respectively.
- Those that are special to mod_rewrite include those below.
+ The variables SCRIPT_FILENAME and REQUEST_FILENAME
+ contain the same value - the value of the
+ filename field of the internal
+ request_rec structure of the Apache HTTP Server.
+ The first name is the commonly known CGI variable name
+ while the second is the appropriate counterpart of
+ REQUEST_URI (which contains the value of the
+ uri field of request_rec).
+
+ If a substitution occurred and the rewriting continues,
+ the value of both variables will be updated accordingly.
+
+ If used in per-server context (i.e., before the
+ request is mapped to the filesystem) SCRIPT_FILENAME and
+ REQUEST_FILENAME cannot contain the full local filesystem
+ path since the path is unknown at this stage of processing.
+ Both variables will initially contain the value of REQUEST_URI
+ in that case. In order to obtain the full local filesystem
+ path of the request in per-server context, use an URL-based
+ look-ahead %{LA-U:REQUEST_FILENAME} to determine
+ the final value of REQUEST_FILENAME.
+
+ Those that are special to mod_rewrite
+ include those below.
API_VERSION
@@ -672,7 +711,7 @@ RewriteRule "^/ex/(.*)" "${examplemap:$1}"
such as "/index.html". This notably excludes the
query string which is available as its own variable
named QUERY_STRING. The value returned for
- REQUEST_URI
+ REQUEST_URI
has already been %-decoded, to re-encode it pass it through
the "escape" mapping-function.
Note that this server variable differs from the CGI
@@ -693,79 +732,65 @@ RewriteRule "^/ex/(.*)" "${examplemap:$1}"
(decoded), unlike most other variables below.
-
-
- If the TestString has the special value expr,
- the CondPattern will be treated as an
- ap_expr. HTTP headers referenced in the
- expression will be added to the Vary header if the novary
- flag is not given.
+
- Other things you should be aware of:
+ Prefixed Variable Lookups
-
- -
-
The variables SCRIPT_FILENAME and REQUEST_FILENAME
- contain the same value - the value of the
- filename field of the internal
- request_rec structure of the Apache HTTP Server.
- The first name is the commonly known CGI variable name
- while the second is the appropriate counterpart of
- REQUEST_URI (which contains the value of the
- uri field of request_rec).
- If a substitution occurred and the rewriting continues,
- the value of both variables will be updated accordingly.
- If used in per-server context (i.e., before the
- request is mapped to the filesystem) SCRIPT_FILENAME and
- REQUEST_FILENAME cannot contain the full local filesystem
- path since the path is unknown at this stage of processing.
- Both variables will initially contain the value of REQUEST_URI
- in that case. In order to obtain the full local filesystem
- path of the request in per-server context, use an URL-based
- look-ahead %{LA-U:REQUEST_FILENAME} to determine
- the final value of REQUEST_FILENAME.
-
- -
-
%{ENV:variable}, where variable can be
- any environment variable, is also available.
- This is looked-up via internal
+ In addition to the server variables above, the
+ %{PREFIX:name} syntax provides
+ access to additional sources:
+
+
+ %{ENV:variable}
+ - Where variable can be
+ any environment variable. This is looked-up via internal
Apache httpd structures and (if not found there) via
-
getenv() from the Apache httpd server process.
+ getenv() from the Apache httpd server process.
- -
-
%{SSL:variable}, where variable is the
+ %{SSL:variable}
+ - Where variable is the
name of an SSL environment
- variable, can be used whether or not
+ variable. This can be used whether or not
mod_ssl is loaded, but will always expand to
the empty string if it is not. Example:
%{SSL:SSL_CIPHER_USEKEYSIZE} may expand to
128. These variables are available even without
setting the StdEnvVars option of the
- SSLOptions directive.
+ SSLOptions directive.
- -
-
%{HTTP:header}, where header can be
- any HTTP MIME-header name, can always be used to obtain the
+ %{HTTP:header}
+ - Where header can be
+ any HTTP MIME-header name. This can always be used to obtain the
value of a header sent in the HTTP request.
Example:
%{HTTP:Proxy-Connection} is
the value of the HTTP header
``Proxy-Connection:''.
- If a HTTP header is used in a condition this header is added to
+
If an HTTP header is used in a condition, this header is added to
the Vary header of the response in case the condition evaluates
to true for the request. It is not added if the
condition evaluates to false for the request. Adding the HTTP header
to the Vary header of the response is needed for proper caching.
It has to be kept in mind that conditions follow a short circuit
logic in the case of the 'ornext|OR' flag
- so that certain conditions might not be evaluated at all.
+ so that certain conditions might not be evaluated at all.
+
+
+
+
+ Look-ahead Sub-requests
- -
-
%{LA-U:variable}
- can be used for look-aheads which perform
- an internal (URL-based) sub-request to determine the final
+ These forms perform an internal sub-request to determine the
+ final value of a variable that is not yet available at the current
+ stage of processing:
+
+
+ %{LA-U:variable}
+ - Performs an internal (URL-based) sub-request to determine the final
value of variable. This can be used to access
- variable for rewriting which is not available at the current
+ a variable for rewriting which is not available at the current
stage, but will be set in a later phase.
For instance, to rewrite according to the
REMOTE_USER variable from within the
@@ -778,14 +803,31 @@ RewriteRule "^/ex/(.*)" "${examplemap:$1}"
its per-directory context via
the Fixup phase of the API and because the authorization
phases come before this phase, you just can use
- %{REMOTE_USER} in that context.
+ %{REMOTE_USER} in that context.
- -
-
%{LA-F:variable} can be used to perform an internal
- (filename-based) sub-request, to determine the final value
- of variable. Most of the time, this is the same as
- LA-U above.
-
+ %{LA-F:variable}
+ - Performs an internal (filename-based) sub-request to determine
+ the final value of variable. Most of the time, this is
+ the same as LA-U above.
+
+
+
+
+ Expression Syntax
+
+ If the TestString has the special value expr,
+ the CondPattern will be treated as an
+ ap_expr. HTTP headers referenced in the
+ expression will be added to the Vary header if the novary
+ flag is not given.
+
+
+
+
+
+ CondPattern
CondPattern is the condition pattern,
a regular expression which is applied to the
@@ -798,16 +840,19 @@ RewriteRule "^/ex/(.*)" "${examplemap:$1}"
additional syntax available to perform other useful tests against
the Teststring:
-
- - You can prefix the pattern string with a
+
+
+
You can prefix the pattern string with a
'!' character (exclamation mark) to negate the result
of the condition, no matter what kind of CondPattern is used.
-
+
+
+
- -
- You can perform lexicographical string comparisons:
+
String Comparisons
-
+
- <CondPattern
- Lexicographically precedes
Treats the CondPattern as a plain string and
@@ -847,22 +892,23 @@ RewriteRule "^/ex/(.*)" "${examplemap:$1}"
if TestString lexicographically follows
CondPattern, or is equal to CondPattern
(the two strings are equal, character for character).
-
+
+
Note
The string comparison operator is part of the CondPattern
argument and must be included in the quotes if those are used. Eg.
-
+
RewriteCond %{HTTP_USER_AGENT} "=This Robot/1.0"
-
-
- -
- You can perform integer comparisons:
-
+
+
+ Integer Comparisons
+
- -eq
- Is numerically equal to
The TestString is treated as an integer, and is
@@ -907,32 +953,27 @@ RewriteCond %{HTTP_USER_AGENT} "=This Robot/1.0"
numerically compared to the CondPattern. True if
the two are numerically different. This is equivalent to
!-eq.
+
-
-
-
- - You can perform various file attribute tests:
-
+
-
+ File Attribute Tests
+
- -d
-
- Is directory.
Treats the TestString as a pathname and tests
whether or not it exists, and is a directory.
- -f
-
- Is regular file.
-
Treats the TestString as a pathname and tests
whether or not it exists, and is a regular file.
-
+
- -F
-
- Is existing file, via subrequest.
Checks whether or not TestString is a valid file,
accessible via all the server's currently-configured
@@ -947,7 +988,6 @@ RewriteCond %{HTTP_USER_AGENT} "=This Robot/1.0"
- -l
-
- Is symbolic link.
Treats the TestString as a pathname and tests
whether or not it exists, and is a symbolic link. May also
@@ -986,19 +1026,20 @@ RewriteCond %{HTTP_USER_AGENT} "=This Robot/1.0"
whether or not it exists, and has executable permissions.
These permissions are determined according to
the underlying OS.
+
-
-
- For example:
+ For example:
RewriteCond /var/www/%{REQUEST_URI} !-f
RewriteRule ^(.+) /other/archive/$1 [R]
-
+
+
+ Expression Evaluation
- -
If the TestString has the special value expr, the
CondPattern will be treated as an
ap_expr.
@@ -1013,28 +1054,32 @@ RewriteRule ^(.+) /other/archive/$1 [R]
RewriteCond expr "! %{HTTP_REFERER} -strmatch '*://%{HTTP_HOST}/*'"
RewriteRule "^/images" "-" [F]
-
-
- You can also set special flags for CondPattern by appending
+
+
+
+
+
Flags
+
+ You can set special flags for CondPattern by appending
[flags]
as the third argument to the RewriteCond
directive, where flags is a comma-separated list of any of the
following flags:
-
-
- - '
nocase|NC'
- (no case)
+
+
+ - '
nocase|NC'
+ - (no case)
This makes the test case-insensitive - differences
between 'A-Z' and 'a-z' are ignored, both in the
expanded TestString and the CondPattern.
This flag is effective only for comparisons between
TestString and CondPattern. It has no
- effect on filesystem and subrequest checks.
+ effect on filesystem and subrequest checks.
- -
- '
ornext|OR'
- (or next condition)
+ - '
ornext|OR'
+ - (or next condition)
Use this to combine rule conditions with a local OR
instead of the implicit AND. Typical example:
@@ -1047,20 +1092,25 @@ RewriteRule ...some special stuff for any of these hosts...
Without this flag you would have to write the condition/rule
pair three times.
-
+
- - '
novary|NV'
- (no vary)
- If a HTTP header is used in the condition, this flag prevents
+ - '
novary|NV'
+ - (no vary)
+ If an HTTP header is used in the condition, this flag prevents
this header from being added to the Vary header of the response.
Using this flag might break proper caching of the response if
the representation of this response varies on the value of this header.
So this flag should be only used if the meaning of the Vary header
is well understood.
-
-
+
+
- Example:
+
+
+
+
+ Example
To rewrite the Homepage of a site according to the
``User-Agent:'' header of the request, you can
@@ -1090,6 +1140,7 @@ RewriteRule "^/$" "/homepage.std.html" [L]
+
RewriteRule
Defines rules for the rewriting engine