Introduction to RewriteCond rules in Apache htaccess

Source: Internet
Author: User

The apache module mod_rewrite provides a rewrite engine based on the regular expression analyzer to rewrite URL requests in real time. It supports a flexible and powerful URL operation mechanism for each complete rule with unlimited number of sub-rules and additional condition rules. This URL operation can depend on various tests, such as server variables, environment variables, HTTP headers, time tags, and even search databases that match URL components.
 
 
This module can operate all parts of the URL (including the path information) at the server level (httpd. conf) and directory-level (. and generate the final request string. The result of this rewrite operation can be internal sub-processing, external request redirection, or even internal proxy processing.
The RewriteCond rules and parameter descriptions are described here. The RewriteCond command defines the condition for the rule to take effect. That is, one or more RewriteCond commands can be executed before one RewriteRule command. The rewrite rule after the condition takes effect only when the current URI matches Pattern and meets the condition (TestString can match CondPattern.
[Description] conditions for rewriting
[Syntax] RewriteCond TestString CondPattern [flags]
Scope: server config, virtual host, directory,. htaccess
[Overwrite] FileInfo
[Status] Scaling (E)
[Module] mod_rewrite
TestString is a plain text string, but it can contain the following extensible components:
RewriteRule reverse reference. The reference method is: $ N (0 <= N <= 9) to reference the current (with several RewriteRule instructions) group Composition matching Pattern in RewriteCond (parentheses !).
RewriteCond reverse reference. The reference method is: % N (1 <= N <= 9) to reference the grouping components (parentheses!) in the last met conditions of the current RewriteCond !).
RewriteMap extension. The reference method is $ {mapname: key | default}. For details, see the RewriteMap command.
Server variable. The reference method is % {NAME_OF_VARIABLE} NAME_OF_VARIABLE. It can be one of the strings listed in the following table:
HTTP header connection and request
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
Server Date and Time others
DOCUMENT_ROOT
SERVER_ADMIN
SERVER_NAME
SERVER_ADDR
SERVER_PORT
SERVER_PROTOCOL
SERVER_SOFTWARE TIME_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 correspond to http mime headers like naming, Apache server C variables, and struct tm fields in Unix systems. Most of these variables are described in other manuals or CGI specifications. The variables specific to mod_rewrite are as follows:
IS_SUBREQ
If the request being processed is a subrequest, it will contain the string "true"; otherwise, it is "false ". In order to parse the additional files in the URI, the module may produce subrequests.
API_VERSION
This is the version of the Apache module API (Internal interface between the server and the module) in use, which is defined in include/ap_mmn.h. The API version of this module corresponds to the Apache version in use (for example, the value of this module is 19990320: 10 in the Apache 1.3.14 release ). Generally, developers are interested in the module.
THE_REQUEST
This is the complete HTTP request line sent by the browser (for example, "GET/index.html HTTP/1.1 ″). It does not contain other header information sent by any browser.
REQUEST_URI
This is the requested resource in the HTTP request line (for example, "/index.html" in the above example ").
REQUEST_FILENAME
This is the complete path name of the local file system that matches the request.
HTTPS
If the connection uses SSL/TLS, it will contain the string "on", otherwise it will be "off" (whether or not mod_ssl has been loaded, this variable can be safely used ).
 
Other considerations:
The SCRIPT_FILENAME and REQUEST_FILENAME values are the same-that is, the filename field in the request_rec structure of the Apache server. The first is the CGI variable name that everyone knows, and the second is a copy of REQUEST_URI (uri field in the request_rec structure.
Special form: % {ENV: variable}, where variable can be any environment variable. It is obtained by querying the internal structure of Apache or by the Apache server process using getenv.
Special form: % {SSL: variable}, where variable can be the name of an SSL environment variable, whether or not the mod_ssl module has been loaded can be used (empty string when not loaded ). For example, % {SSL: SSL_CIPHER_USEKEYSIZE} will be replaced with 128.
Special format: % {HTTP: header}. The header can be the name of any http mime header. It can always be obtained by searching for HTTP requests. For example, % {HTTP: Proxy-Connection} will be replaced with the value of Proxy-Connection: HTTP header.
Default form: % {LA-U: variable}, final value of variable is determined after an internal (URL-based) subrequest is executed. You can use this method when you need to use a variable that is currently unknown but will be set later. For example, you must use % {LA-U: REMOTE_USER} to override the REMOTE_USER variable in the server-level configuration (httpd. conf file }. Because this variable is rewritten by the URL (mod ?? _ Rewrite. On the other hand, because mod_rewrite uses API correction steps to implement directory-level (. htaccess file) configuration, authentication steps are prior to API correction steps, so % {REMOTE_USER} can be used }.
Default form: % {LA-F: variable}, final value of variable is determined after an internal (file name-based) subrequest is executed. In most cases it is the same as the above LA-U.
 
CondPattern is a condition mode, that is, a regular expression applied to the current TestString instance. TestString is calculated first and then matched with CondPattern.
Note: CondPattern is a perl-Compatible Regular Expression, but there are several additions:
1. You can use '! '(Exclamation point) to specify the mismatch.
2. CondPatterns has several special variants. In addition to the standard usage of regular expressions, there are also the following usage:
'<CondPattern' (The dictionary order is smaller)
Consider CondPattern as a pure string and compare it with TestString in alphabetical order. If TestString is smaller than CondPattern, it is true.
'> CondPattern' (greater than the dictionary order)
Consider CondPattern as a pure string and compare it with TestString in alphabetical order. If TestString is greater than CondPattern, it is true.
'= CondPattern' (equal to the dictionary order)
Consider CondPattern as a pure string and compare it with TestString in alphabetical order. If TestString is equal to CondPattern (two strings are exactly the same one by character), it is true. If CondPattern is "" (two double quotation marks), TestString will be compared with an empty string.
'-D' (directory)
Treat TestString as a path name and test whether it is an existing Directory.
'-F' (regular file)
Treat TestString as a path name and test whether it is an existing regular file.
'-S' (non-empty regular files)
Consider TestString as a path name and test whether it is an existing regular file with a size greater than 0.
'-L' (symbolic join)
Treat TestString as a path name and test whether it is an existing symbolic connection.
'-X' (executable)
Treat TestString as a path name and test whether it is an existing file with executable permissions. This permission is detected by the operating system.
'-F' (the file that exists in the subrequest)
Check whether TestString is a valid file and can be accessed under the current access control configuration of the server. It uses an internal sub-request for check. Please use it with caution because it will reduce the server performance!
'-U' (the URL of the Child request)
Check whether TestString is a valid URL and can be accessed under the current access control configuration of the server. It uses an internal sub-request for check. Please use it with caution because it will reduce the server performance!
Note: all these tests can be prefixed with exclamation marks ('! ') To reverse the test conditions.
3. You can append the special mark [flags] After the CondPattern command as the third parameter of the RewriteCond command. Flags is a list of the following tags separated by commas:
'Nocase | NC '(Case Insensitive)
It makes the test case insensitive. The expanded TestString and 'az' in CondPattern are no different from those in 'a-Z. This flag is only used for comparison between TestString and CondPattern, but does not work for checks on the file system and subrequests.
'Ornext | OR '(OR the next part)
It combines conditions of several rules in the OR mode, rather than implicit AND. A typical example is as follows:
RewriteCond % {REMOTE_HOST} ^ host1. * [OR]
RewriteCond % {REMOTE_HOST} ^ host2. * [OR]
RewriteCond % {REMOTE_HOST} ^ host3 .*
RewriteRule... Rule Set for these three hosts... If this tag is not used, you must write three condition/Rule pairs.
Example
If you want to rewrite the home page of a site according to "User-Agent:" In the request header, you can write as follows:
RewriteCond % {HTTP_USER_AGENT} ^ Mozilla. * RewriteRule ^/$/homepage. max. html [L]
 
RewriteCond % {HTTP_USER_AGENT} ^ Lynx. * RewriteRule ^/$/homepage. min. html [L]
 
RewriteRule ^/$/homepage .std.html [L]
Explanation:
If your browser recognizes the flag as 'mozilla ', you will get the homepage (including Frames and so on) that maximizes the content ).
If you are using (terminal-based) Lynx, what you get is the homepage with minimal content (excluding tables and so on ).
If none of the above conditions is met (other browsers are used), you will get a standard homepage.

From The 0th Space

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.