The main function of Rewirte is to implement URL jump. Its regular expression is based on the Perl language. It can be based on the server-level (httpd. conf) and directory-level (. htaccess) methods. To use the rewrite module, you must first install or load the rewrite module. There are two ways to install the rewrite module directly when compiling apache. The other is to install apache in DSO mode when compiling apache, and then use the source code and apxs to install the rewrite module.
Server-level (httpd. conf) There are two methods, one is in httpd. in conf, RewriteEngine on is directly used to enable the rewrite function. In addition, the RewriteEngine on is used to enable the rewrite function locally. The following is an example, you must use RewriteEngine on in each virtualhost to enable the rewrite function. Otherwise, rules in virtualhost without RewriteEngine on will not take effect.
For directory-based (. htaccess), note that the FollowSymLinks attribute of this directory must be opened and RewriteEngine on must be declared in. htaccess.
Practice
Example:
The Code is as follows: |
Copy code |
RewriteEngine on RewriteCond % {HTTP_USER_AGENT} ^ MSIE [NC, OR] RewriteCond % {HTTP_USER_AGENT} ^ Opera [NC] RewriteRule ^. *-[F, L] Here "-" indicates no replacement. Visitors with IE and Opera browsers will be banned from accessing the site. |
Example:
The Code is as follows: |
Copy code |
RewriteEngine On RewriteBase/test RewriteCond % {REQUEST_FILENAME}. php-f RewriteRule ([^/] +) $/test/$ 1.php # For example:/test/admin =>/test/admin. php RewriteRule ([^/]+).html $/test/$ 1.php [L] # For example:/test/admin.html =>/test/admin. php |
Use Apache URL Rewrite to configure a Multi-User Virtual Server
To implement this function, you must first enable wildcard domain name resolution on the DNS server (by yourself or by looking for a Domain Name Service Provider ). For example, I will resolve all *. kiya. us and * .kiya.cn to my IP address 70.40.213.183.
Next, let's take a look at the setting of *. kiya. us virtual host in my Apache.
The Code is as follows: |
Copy code |
ServerAdmin webmaster@kiya.us DocumentRoot/home/www/bKjia. c0m ServerName dns. kiya. us ServerAlias dns. kiya. us kiya. us *. kiya. us CustomLog/var/log/httpd/osa/access_log.log "common ErrorLog/var/log/httpd/osa/error_log.log" AllowOverride None Order deny, allow # Adddefacharcharset GB2312 RewriteEngine on RewriteCond % {HTTP_HOST} ^ [^.] +. kiya. (cn | us) $ RewriteRule ^ (. +) % {HTTP_HOST} $1 [C] RewriteRule ^ ([^.] +). kiya. (cn | us) (. *) $/home/www/bKjia. c0m/sylvan $3? Un = $1 & % {QUERY_STRING} [L] |
In this section, I set the Document Root of * .kiya.cn and *. kiya. us To/home/www/bKjia. c0m.
Restrict directories to display images only
The Code is as follows: |
Copy code |
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond % {REQUEST_FILENAME }! ^. *. (Gif | jpg | jpeg | png | swf) $ RewriteRule. * $-[F, L] </IfModule> |
Description of the RewriteRule rule expression:
. Match any single character
[Chars] matched string: chars
[^ Chars] unmatched string: chars
Text1 | string that can be selected by text2: text1 or text2
? Matches 0 to 1 Characters
* Matches 0 to multiple characters
+ Match 1 to multiple characters
^ String start flag
$ String end flag
N Escape Character Mark
Reverse reference $ N is used to call the matching variable in RewriteRule (0 <= N <= 9)
Reverse reference % N is used to call the last matched variable in RewriteCond (1 <= N <= 9)
Applicable identifier of RewriteCond
'Nocase | NC '(no case) Ignore size
'Ornext | or' (OR next condition) logic or, multiple RewriteCond conditions can be matched simultaneously
Applicable identifier of the RewriteRule
'Redirect | R [= code] '(force redirect) force rewrite to an external redirection starting with http (note URL changes) such as: [R = 301, L]
'Forbidden | f' (force URL to be forbidden) is rewritten to prohibit access.
'Proxy | P' (force proxy) is rewritten to the http path accessed through the proxy
'Last | l' (last rule) indicates the last rewrite rule flag. If it matches, it will not be executed.
'Next | n' (next round) loops through the same rule until it cannot match
'Chain | C' (chained with next rule) If this rule is matched, the following chain-marked rule is continued.
'Type | T = MIME-type' (force MIME type) specifies the MIME type
'Nosubreq | ns' (used only if no internal sub-request)
'Nocase | NC '(no case) Ignore size
'Qsappend | qsa' (query string append) append the query string.
'Noescape | ne' (no URI escaping of output) disables characters in the URL from being automatically converted to % [0-9] +.
'Passthrough through to next handler 'applies the rewriting result to mod_alias
'Skip | S = num' (skip next rule (s) skips the following rules
'Env | E = VAR: VAL '(set environment variable) to add environment Variables
Overview of Apache mod_rewrite rule rewriting flag
1) R [= code] (force redirect) force external redirection
Force the replacement string to add http: // thishost [: thisport]/prefix to the external URL. If the code is not specified, the default 302 HTTP status code will be used.
2) F (force URL to be forbidden) disables the URL and returns a 403HTTP status code.
3) G (force URL to be gone) forces the URL to be GONE and returns the response HTTP status code.
4) P (force proxy) enforces proxy forwarding.
5) L (last rule) indicates that the current rule is the last rule. After the analysis is stopped, the rule is overwritten.
6) N (next round) re-runs the rewrite process from the first rule.
7) C (chained with next rule) is associated with the next rule
If the rule matches, it is processed normally and the flag is invalid. If the rule does not match, all associated rules are skipped.
8) T = MIME-type (force MIME type) force MIME type
9) NS (used only if no internal sub-request) is used only for non-internal subrequests
10) NC (no case) is case insensitive
11) QSA (query string append) append request string
12) NE (no URI escaping of output) does not escape special characters
Example: RewriteRule/foo/(. *)/bar? Arg = P1 % 3d $1 [R, NE] will be able to correctly convert/foo/zoo to/bar? Arg = P1 = zoo
13) pass through to next handler to the next Processing
For example:
RewriteRule ^/abc (. *)/def $1 [PT] # will be handed over to/def rules for processing
Alias/def/ghi
14) S = num (skip next rule (s) skipping num rules
15) E = VAR: VAL (set environment variable) set environment Variables