RewriteCond directive

Description: Defines a condition for the subsequent RewriteRule
Syntax: RewriteCond TestString CondPattern
Context: server config, virtual host, directory, .htaccess

 

This directive defines a single condition for the following RewriteRule, RewriteHeader or RewriteProxy directive. There can be one or more conditions preceding a rule directive and the rule will only apply if all of the conditions are met.

TestString additionally to plain text can contain the following constructs:

  • Back references to RewriteRule pattern using syntax $N.
  • Back references to preceding RewriteCond patterns using syntax %N.
  • RewriteMap expansions using syntax ${mapname:key|default}.
  • HTTP header value with the syntax %{HTTP:header}.
  • Server variable using the syntax %{ NAME_OF_VARIABLE }.
    Here is a list of available server variables:|

    HTTP_USER_AGENT
    HTTP_REFERER
    HTTP_COOKIE
    HTTP_FORWARDED
    HTTP_HOST
    HTTP_PROXY_CONNECTION
    HTTP_ACCEPT

    REMOTE_ADDR
    REMOTE_HOST
    REMOTE_PORT
    REMOTE_USER
    REMOTE_IDENT
    REQUEST_METHOD
    SCRIPT_FILENAME
    PATH_INFO
    QUERY_STRING
    AUTH_TYPE

    DOCUMENT_ROOT
    SERVER_NAME
    SERVER_ADDR
    SERVER_PORT
    SERVER_PROTOCOL
    SERVER_SOFTWARE

    API_VERSION
    THE_REQUEST
    REQUEST_URI
    REQUEST_FILENAME
    HTTPS

    TIME_YEAR
    TIME_MON
    TIME_DAY
    TIME_HOUR
    TIME_MIN
    TIME_SEC
    TIME_WDAY
    TIME

    Additionally all special IIS server variables are supported.

CondPattern specifies a regular expression that will be applied to the instance of TestString. Following special values are also supported:

  1. Prefix regular expression with '!' symbol to specify negation pattern.
  2. '<CondPattern' Treats CondPattern as a plain string that will be lexicographically compared as more than.
  3. '>CondPattern' Lexicographically less then comparison.
  4. '=CondPattern' Lexicographically equals comparison.
  5. '-d' Test string is existing directory.
  6. '-f' Test string is existing file.
  7. '-s' Test string is a file with nonzero size.
  8. '-l' (link) Unsupported, always false.
  9. '-x' (has executable permissions) Unsupported, always true.
  10. '-F' (is existing file, via subrequest) Unsupported, same as '–f'.
  11. '-U' (is existing URL, via subrequest) Unsupported, always false.

The following flags are also supported:

  • 'nocase|NC'

    This flag makes the Pattern case-insensitive.
     
  • 'ornext|OR'

    This flag combines subsequent RewriteCond directives with logical OR instead of implicit AND.
     
  • O

    Leaved for compatibility with ISAPI_Rewrite 2.x. Normalizes string before processing. Normalization includes removing of an URL-encoding, illegal characters, etc. Also, IIS normalization of a URL completely removes query string from it.