| Директива RewriteCond
Description: | Defines a condition under which rewriting will take place
|
Syntax: | RewriteCond
TestString CondPattern [flags] |
Context: | server config, virtual host, directory, .htaccess |
Override: | FileInfo |
Status: | Extension |
Module: | mod_rewrite |
The RewriteCond directive defines a
rule condition. One or more RewriteCond
can precede a RewriteRule
directive. The following rule is then only used if both
the current state of the URI matches its pattern, and if these conditions are met.
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
(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.
-
RewriteCond backreferences: These are
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.
-
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:
HTTP headers: | connection & request: | |
HTTP_ACCEPT
HTTP_COOKIE
HTTP_FORWARDED
HTTP_HOST
HTTP_PROXY_CONNECTION
HTTP_REFERER
HTTP_USER_AGENT
|
AUTH_TYPE
CONN_REMOTE_ADDR
CONTEXT_PREFIX
CONTEXT_DOCUMENT_ROOT
IPV6
PATH_INFO
QUERY_STRING
REMOTE_ADDR
REMOTE_HOST
REMOTE_IDENT
REMOTE_PORT
REMOTE_USER
REQUEST_METHOD
SCRIPT_FILENAME
|
|
server internals: | date and time: | specials: |
DOCUMENT_ROOT
SCRIPT_GROUP
SCRIPT_USER
SERVER_ADDR
SERVER_ADMIN
SERVER_NAME
SERVER_PORT
SERVER_PROTOCOL
SERVER_SOFTWARE
|
TIME_YEAR
TIME_MON
TIME_DAY
TIME_HOUR
TIME_MIN
TIME_SEC
TIME_WDAY
TIME
|
API_VERSION
CONN_REMOTE_ADDR
HTTPS
IS_SUBREQ
REMOTE_ADDR
REQUEST_FILENAME
REQUEST_SCHEME
REQUEST_URI
THE_REQUEST
|
These variables all
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 here
or elsewhere in the Manual or in the CGI specification.
SERVER_NAME and SERVER_PORT depend on the values of
UseCanonicalName and
UseCanonicalPhysicalPort
respectively.
Those that are special to mod_rewrite include those below.
-
API_VERSION
- This is the version of the Apache httpd module API
(the internal interface between server and
module) in the current httpd build, as defined in
include/ap_mmn.h. The module API version
corresponds to the version of Apache httpd in use (in
the release version of Apache httpd 1.3.14, for
instance, it is 19990320:10), but is mainly of
interest to module authors.
-
CONN_REMOTE_ADDR
- Since 2.4.8: The peer IP address of the connection (see the
mod_remoteip module).
-
HTTPS
- Will contain the text "on" if the connection is
using SSL/TLS, or "off" otherwise. (This variable
can be safely used regardless of whether or not
mod_ssl is loaded).
-
IS_SUBREQ
- Will contain the text "true" if the request
currently being processed is a sub-request,
"false" otherwise. Sub-requests may be generated
by modules that need to resolve additional files
or URIs in order to complete their tasks.
-
REMOTE_ADDR
- The IP address of the remote host (see the
mod_remoteip module).
-
REQUEST_FILENAME
- The full local filesystem path to the file or
script matching the request, if this has already
been determined by the server at the time
REQUEST_FILENAME is referenced. Otherwise,
such as when used in virtual host context, the same
value as REQUEST_URI . Depending on the value of
AcceptPathInfo , the
server may have only used some leading components of the
REQUEST_URI to map the request to a file.
-
REQUEST_SCHEME
- Will contain the scheme of the request (usually
"http" or "https"). This value can be influenced with
ServerName .
-
REQUEST_URI
- The path component of the requested URI,
such as "/index.html". This notably excludes the
query string which is available as its own variable
named
QUERY_STRING .
-
THE_REQUEST
- The full HTTP request line sent by the
browser to the server (e.g., "
GET
/index.html HTTP/1.1 "). This does not
include any additional headers sent by the
browser. This value has not been unescaped
(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:
-
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
Apache httpd structures and (if not found there) via
getenv() from the Apache httpd server process.
-
%{SSL:variable} , where variable is the
name of an SSL environment
variable, 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.
-
%{HTTP:header} , where header can be
any HTTP MIME-header name, 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
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.
-
%{LA-U:variable}
can be used for look-aheads which perform
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
stage, but will be set in a later phase.
For instance, to rewrite according to the
REMOTE_USER variable from within the
per-server context ( apache2.conf file) you must
use %{LA-U:REMOTE_USER} - this
variable is set by the authorization phases, which come
after the URL translation phase (during which mod_rewrite
operates).
On the other hand, because mod_rewrite implements
its per-directory context ( .htaccess file) 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.
-
%{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.
CondPattern is the condition pattern,
a regular expression which is applied to the
current instance of the TestString.
TestString is first evaluated, before being matched against
CondPattern.
CondPattern is usually a
perl compatible regular expression, but there is
additional syntax available to perform other useful tests against
the Teststring:
- 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:
- <CondPattern
- Lexicographically precedes
Treats the CondPattern as a plain string and
compares it lexicographically to TestString. True if
TestString lexicographically precedes
CondPattern.
- >CondPattern
- Lexicographically follows
Treats the CondPattern as a plain string and
compares it lexicographically to TestString. True if
TestString lexicographically follows
CondPattern.
- =CondPattern
- Lexicographically equal
Treats the CondPattern as a plain string and
compares it lexicographically to TestString. True if
TestString is lexicographically equal to
CondPattern (the two strings are exactly
equal, character for character). If CondPattern
is "" (two quotation marks) this
compares TestString to the empty string.
- <=CondPattern
- Lexicographically less than or equal to
Treats the CondPattern as a plain string and
compares it lexicographically to TestString. True
if TestString lexicographically precedes
CondPattern, or is equal to CondPattern
(the two strings are equal, character for character).
- >=CondPattern
- Lexicographically greater than or equal to
Treats the CondPattern as a plain string and
compares it lexicographically to TestString. True
if TestString lexicographically follows
CondPattern, or is equal to CondPattern
(the two strings are equal, character for character).
-
You can perform integer comparisons:
- -eq
- Is numerically equal to
The TestString is treated as an integer, and is
numerically compared to the CondPattern. True if
the two are numerically equal.
- -ge
- Is numerically greater than or equal to
The TestString is treated as an integer, and is
numerically compared to the CondPattern. True if
the TestString is numerically greater than or equal
to the CondPattern.
- -gt
- Is numerically greater than
The TestString is treated as an integer, and is
numerically compared to the CondPattern. True if
the TestString is numerically greater than
the CondPattern.
- -le
- Is numerically less than or equal to
The TestString is treated as an integer, and is
numerically compared to the CondPattern. True if
the TestString is numerically less than or equal
to the CondPattern. Avoid confusion with the
-l by using the -L or
-h variant.
- -lt
- Is numerically less than
The TestString is treated as an integer, and is
numerically compared to the CondPattern. True if
the TestString is numerically less than
the CondPattern. Avoid confusion with the
-l by using the -L or
-h variant.
- -ne
- Is numerically not equal to
The TestString is treated as an integer, and is
numerically compared to the CondPattern. True if
the two are numerically different. This is equivalent to
!-eq .
- You can perform various 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
access controls for that path. This uses an internal
subrequest to do the check, so use it with care -
it can impact your server's performance!
- -h
- Is symbolic link, bash convention.
See -l.
- -l
- Is symbolic link.
Treats the TestString as a pathname and tests
whether or not it exists, and is a symbolic link. May also
use the bash convention of -L or
-h if there's a possibility of confusion
such as when using the -lt or
-le tests.
- -L
- Is symbolic link, bash convention.
See -l.
- -s
- Is regular file, with size.
Treats the TestString as a pathname and tests
whether or not it exists, and is a regular file with size greater
than zero.
- -U
Is existing URL, via subrequest.
Checks whether or not TestString is a valid URL,
accessible via all the server's currently-configured
access controls for that path. This uses an internal
subrequest to do the check, so use it with care -
it can impact your server's performance!
This flag only returns information about things
like access control, authentication, and authorization. This flag
does not return information about the status code the
configured handler (static file, CGI, proxy, etc.) would have
returned.
- -x
- Has executable permissions.
Treats the TestString as a pathname and tests
whether or not it exists, and has executable permissions.
These permissions are determined according to
the underlying OS.
For example:
RewriteCond /var/www/%{REQUEST_URI} !-f
RewriteRule ^(.+) /other/archive/$1 [R]
-
If the TestString has the special value expr , the
CondPattern will be treated as an
ap_expr.
In the below example, -strmatch is used to
compare the REFERER against the site hostname,
to block unwanted hotlinking.
RewriteCond expr "! %{HTTP_REFERER} -strmatch '*://%{HTTP_HOST}/*'"
RewriteRule "^/images" "-" [F]
You can also 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:
Example:
To rewrite the Homepage of a site according to the
`` User-Agent: '' header of the request, you can
use the following:
RewriteCond "%{HTTP_USER_AGENT}" "(iPhone|Blackberry|Android)"
RewriteRule "^/$" "/homepage.mobile.html" [L]
RewriteRule "^/$" "/homepage.std.html" [L]
Explanation: If you use a browser which identifies itself
as a mobile browser (note that the example is incomplete, as
there are many other mobile platforms), the mobile version of
the homepage is served. Otherwise, the standard page is served.
|
|