Apache RewriteCond instructions explain

  

When applying mod_rewrite in Apache, we often need to define the rewrite rules for .htaccess files, and the RewriteCond directive defines the conditions under which the rewrite rules take effect, that is, before a RewriteRule instruction One or more RewriteCond instructions. The rewrite rule after the condition only works if the current URI matches the Pattern and meets the conditions here. It is not difficult to find that the RewriteCond directive is very similar to the conditional expression of the if statement in PHP.

Definition of RewriteCond Instruction
Description
Defining Conditions for Rewriting to occur Syntax
RewriteCond TestString
CondPattern
[< Em>flags
] Scope
server config, virtual host, directory, .htaccess Overrides
FileInfo Status
Extension(E) Module< Br> mod_rewrite

It can be seen that the RewriteCond directive defines the conditions under which the rewrite rule takes effect. The meaning of the entire RewriteCond statement is: if a test string (TestString) satisfies the conditional pattern (CondPattern), then it is followed. RewriteRule.

TestString

of the RewriteCond directive TestString is a plain text (test) string, but can also contain the following extensible components:

1, RewriteRule reverse Reference, reference method is:

$N (0<= N<= 9) refers to the current (with several RewriteRule instructions) RewriteCond matching Pattern component (the subpattern in the regular expression) ).

2, RewriteCond back reference, the reference method is:

%N(1<= N<=9) refers to the grouping component in the last matching condition in several current RewriteCond conditions (regular Subpattern in the expression).

3, server variables, reference method is:

%{NAME_OF_VARIABLE}

NAME_OF_VARIABLE can be one of the strings listed in the following table:
HTTP header
Connection and request

HTTP_USER_AGENTHTTP_REFERER

HTTP_COOKIE

HTTP_FORWARDED

HTTP_HOST

HTTP_PROXY_CONNECTION

HTTP_ACCEPT

REMOTE_ADDRREMOTE_HOST

REMOTE_PORT

REMOTE_USER

REMOTE_IDENT

REQUEST_METHOD

SCRIPT_FILENAME

PATH_INFO

QUERY_STRING

AUTH_TYPE
Server itself
Date and time
Other

DOCUMENT_ROOT

SERVER_ADMIN

SERVER_NAME

SERVER_ADDR

SERVER_PORT

SERVER_PROTOCOL

SERVER_SOFTWARE

T IME_YEAR

TIME_MON

TIME_DAY

TIME_HOUR

TIME_MIN

TIME_SEC

TIME_WDAY

TIME

API_VERSION

THE_REQUEST

REQUEST_URI

REQUEST_FILENAME

IS_SUBREQ

HTTPS

These variables Both correspond to similarly named HTTP MIME headers, C variables for Apache servers, and struct tm fields for Unix systems, most of which are described in other manuals or CGI specifications.

The condPattern of the RewriteCond directive

CondPattern is a conditional pattern, a regular expression applied to the current TestString instance. TestString will be evaluated first and then matched with CondPattern.

You can use the '!' (exclamation point) at the beginning of the CondPattern string to specify a mismatch.

CondPatterns is a perl-compatible regular expression with a few special variants. In addition to the standard usage of regular expressions, there are several additions:

1, '<CondPattern' (less than the lexicographic order) treats CondPattern as a pure string, compared to TestString in lexicographic order. True if TestString is less than CondPattern.

2, '>CondPattern' (more than the lexicographic order) treats CondPattern as a pure string, compared to TestString in lexicographic order. True if TestString is greater than CondPattern.

3, '=CondPattern' (equal to the dictionary order) treats CondPattern as a pure string, compared to TestString in lexicographic order. True if TestString is equal to CondPattern (the two strings are exactly equal character by character). If CondPattern is "" (two double quotes), TestString will be compared to the empty string.

4, '-d' (directory) treats TestString as a pathname and tests if it is an existing directory.

5, '-f' (regular file) treats TestString as a pathname and tests if it is an existing regular file.

6, '-s' (a non-empty regular file) treats TestString as a pathname and tests if it is an existing regular file with a size greater than zero.

7, '-l' (symbolic connection) treats TestString as a pathname and tests if it is an existing symbolic link.

8, '-x' (executable) treats TestString as a pathname and tests if it is an existing file with executable permissions. This permission is detected by the operating system.

9, '-F' (the file that exists for the subrequest) checks if TestString is a valid file and can be accessed under the server's current access control configuration. It uses an internal subrequest to check, because it will reduce the performance of the server, so use it with caution!

10, '-U' (URL for the existence of the subrequest) Check if TestString is a valid URL And can be accessed under the server's current access control configuration. It uses an internal subrequest to check it, so it will reduce the performance of the server, so use it with caution!

Note: All these tests can be prefixed with an exclamation point ('!') to achieve the opposite of the test condition. Turn, and you can also append a special flag [flags] after CondPattern as the third parameter of the RewriteCond directive.

Flags of the RewriteCond directive

flags are a comma-separated list of the following tags:

1, 'nocase| NC' (ignoring case): It makes the test ignore case, and there is no difference between 'A-Z' and 'a-z' in the extended TestString and CondPattern. This tag is only used for comparisons between TestString and CondPattern, but not for file system and subrequests.

2,'ornext| OR' (or next condition): It combines the conditions of several rules in OR, not the implicit AND.

RewriteCond Instance

RewriteCond %{REMOTE_HOST} ^host1.* [OR]

RewriteCond %{REMOTE_HOST} ^host2.* [OR]

RewriteCond %{REMOTE_HOST} ^host3.*

RewriteRule ... rule set for these 3 hosts...

Copyright © Windows knowledge All Rights Reserved