Apache. Документация на русском


Директивы Apache
  1    2    3    4    5    6    7    8    9    10    11    12    13    14    15    16    17    18    19    20    21    22    23    24    25    26    27    28    29    30    31    32    33    34    35    36    37    38    39    40    41    42    43    44    45    46    47    48    49    50    51    52    53    54    55    56    57    58    59    60    61    62    63    64    65    66    67    68    69    70    71    72    73    74    75    76    77    78    79    80    81    82    83    84    85  
  86    87      88      89    90    91    92  
  93    94    95    96    97    98    99    100    101    102    103    104    105    106    107    108    109    110    111    112    113    114    115    116    117    118    119    120    121    122    123    124    125    126    127    128    129    130    131    132    133    134    135    136    137    138    139    140    141    142    143    144    145    146    147    148    149    150    151    152    153    154    155    156    157    158    159    160    161    162    163    164    165  
  166    167    168    169    170    171    172    173    174    175    176    177    178    179    180    181    182    183    184    185    186    187    188    189    190    191    192    193    194    195    196    197    198    199    200    201    202    203    204    205    206    207    208    209    210    211    212    213    214    215    216    217    218    219    220    221    222    223    224    225    226    227    228    229    230    231    232    233    234    235    236    237    238    239    240    241    242  

 <         > 
Список директив: Core  |  ModRewrite  |  Lua  |  Proxy  |  SSL

Директива RewriteCond
RU          EN  

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:

  1. 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.

  2. %{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.
  3. %{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.
  4. %{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.

  5. %{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.

  6. %{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:

  1. 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.
  2. 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).
  3. 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 .
  4. 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]
  5. 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:

  • ' 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.
  • ' ornext|OR ' (or next condition)
    Use this to combine rule conditions with a local OR instead of the implicit AND. Typical example:
    RewriteCond "%{REMOTE_HOST}" "^host1" [OR]
    RewriteCond "%{REMOTE_HOST}" "^host2" [OR]
    RewriteCond "%{REMOTE_HOST}" "^host3"
    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 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:

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.

RU          EN  


Рейтинг@Mail.ru