Defining Rewriting Conditions
To configure RewriteCond, specify rewriting conditions as follows. Compare the TestString and CondPattern. If the conditions are met, RewriteCond is changed to the pattern defined in RewriteRule configuration.
RewriteCond <TestString> <CondPattern> [flags]
TestString Configuration
In TestString, the following reserved words and general strings can be used.
-
$N (0 <= N <= 9)
Refer to the pattern wrapped in parentheses among RewriteRule’s patterns.
-
%N (1 <= N <= 9)
Refer to the pattern wrapped in parentheses among RewriteCond’s CondPatterns.
$N and %N have the following Regex Back-Reference structure:
-
%{SERVER_VARIABLE_NAME}
Refer to the additional variables among the server environment variables used in CGI.
Type Variable Server Environment Variable
%{ENV:variable}: Refers to environment variables, %{HTTP:header}: Refers to HTTP request headers
HTTP headers
HTTP_USER_AGENT, HTTP_REFERER, HTTP_COOKIE, HTTP_FORWARDED, HTTP_HOST, HTTP_PROXY_CONNECTION, HTTP_ACCEPT
Connection & request
REMOTE_ADDR, REMOTE_PORT, REMOTE_METHOD, QUERY_STRING, AUTH_TYPE
Server internals
DOCUMENT_ROOT, SERVER_NAME, SERVER_ADDR, SERVER_PORT, SERVER_PROTOCOL, SERVER_SOFTWARE
Date and time
TIME_YEAR, TIME_MON, TIME_DAY, TIME_HOUR, TIME_SEC, TIME_WDAY, TIME
Specials
THE_REQUEST, REQUEST_URI, HTTPS
The following variables provided by Apache’s mod_rewrite cannot be used.
%{SSL:variable}, %{LA-U:variable}, %{LA-F:variable}, REMOTE_HOST, REMOTE_IDENT, SCRIPT_FILENAME, SCRIPT_USER, SCRIPT_GROUP, SERVER_ADMIN, API_VERSION, REQUEST_FILENAME, IS_SUBREQ
CondPattern Configuration
CondPattern can use Perl compatible regular expressions. The following additional patterns can also be used.
Directive | Description |
---|---|
!Pattern |
Specifies the cases that do not match the specified pattern(s). |
<CondPattern |
Matches when TestString is before CondPattern alphabetically. |
>CondPattern |
Matches when TestString is after CondPattern alphabetically. |
=CondPattern |
Matches when TestString is same as CondPattern. |
-d |
Matches when TestString is considered a path and it is a directory. |
-f |
Matches when TestString is considered a path and it is a file. |
-s |
Matches when TestString is considered a path and the file size is greater than 0. |
-l |
Matches when TestString is considered a path and it is a symbolic link. |
-x |
Matches when TestString is considered a path and it is an executable file. |
-F |
Matches when TestString is considered a path and it is accessible in server configuration. |
-U |
Matches when TestString is considered a URL and it is accessible in server configuration. |
All of these patterns can be prefixed by an exclamation mark '!' to negate their meaning. |
The following are the directives used in regular expressions.
Directive | Description |
---|---|
. |
Matches any single character. |
+ |
Repeats the previous pattern one or more times. |
* |
Repeats the previous pattern zero or more times. |
? |
Optionally matches the previous pattern. |
^ |
Optionally matches the previous pattern. |
$ |
Matches the end of the string. |
( ) |
Groups multiple characters into a single unit, and captures the match results for use in backreferences. |
[ ] |
A character class - matches one of the characters. |
[^ ] |
A negative character class - matches any character not specified. |
Flags Configuration
You can change the way patterns are matched by using [flags] in CondPattern.
Directive | Description |
---|---|
nocase|NC |
Not case-sensitive in pattern matching. |
ornext|OR |
If a different RewriteCond exists after RewriteCond, combine the next RewriteCond with the logical OR operator. If not explicitly specified, the next condition is combined with AND. |
novary|NV |
If HTTP Header is used as TestString, do not add Vary Header to Response. |
The following is an example of using the [OR] flag:
RewriteCond %{HTTP_HOST} ^host1.* [OR] RewriteCond %{HTTP_HOST} ^host2.* [OR] RewriteCond %{HTTP_HOST} ^host3.* RewriteRule ...
If the [OR] flag is not used, each RewriteCond/RewriteRule must be written separately.