Introduction of rewrite rules:
Rewirte's main function is to implement a URL jump, and its regular expression is based on the Perl language. Can be based on both server-level (httpd.conf) and directory-level (. htaccess) methods. If you want to use the rewrite module, you must first install or load the rewrite module. There are two ways to compile Apache when the direct installation of rewrite modules, not one is compiled Apache in the DSO mode to install Apache, and then use the source code and APXS to install the rewrite module.
Second, enable rewrite in the Apache configuration
Open configuration file httpd.conf:
Copy Code code as follows:
1. Enable rewrite
# LoadModule Rewrite_module modules/mod_rewrite.so Remove the front #
2. Enable. htaccess
In a virtual machine configuration item
AllowOverride None modified to: allowoverride all
second, rewrite basic writing
The server has a configuration file can not be changed by us, so in most cases to the root directory of the site to build a. htaccess file.
Copy Code code as follows:
Rewriteengine on//start rewrite engine
Rewriterule ^/index ([0-9]*). html$/index.php?id=$1//"([0-9]*)" Representative range with (. *) represents all, hereinafter.
Rewriterule ^/index ([0-9]*)/$/index.php?id=$1 [R]//virtual directory
three, the Apache mod_rewrite rule rewrite the mark list
1) R[=code] (Force redirect) force external redirection
Forces a URL to be redirected to an external string, plus the http://thishost[:thisport]/prefix. If code is not specified, the default 302 HTTP status code is used.
2 F (force URL to is forbidden) disables the URL and returns a 403HTTP status code.
3 G (force URL to is gone) force URL to gone, return 410HTTP status code.
4) P (force proxy) enforces the use of proxy forwarding.
5 L (last rule) indicates that the current rules are the final rule, stopping the parsing of the rules after the analysis.
6) N (next round) again runs the rewrite process from the first rule.
7) C (chained with next) is associated with the next rule
If the rule match is handled normally, the flag is invalid and if it does not match, all of the following 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 not internal child requests
NC (no case) is case-insensitive
QSA (query string append) Append request string
NE (no URI escaping of output) not escaping special characters in output
For example: rewriterule/foo/(. *)/bar?arg=p1%3d$1 [R,ne] will be able to correctly convert/foo/zoo to/bar?arg=p1=zoo
PT (pass through to next handler) passed to next processing
For example:
Copy Code code as follows:
Rewriterule ^/abc (. *)/def$1 [PT] # will be given to/def rule processing
Alias/def/ghi
S=num (skip next rule (S)) skips num Bar rules
E=var:val (Set environment variable) set environment variable
Four, Apache rewrite example
Example one:
At the same time meet the following two requirements:
1. Access to http://www.jb51.net/xxx/with http://www.jb51.net/xxx.php
2. Use Http://yyy.jb51.net to access the HTTP://WWW.JB51.NET/USER.PHP?USERNAME=YYY function
Copy Code code as follows:
Rewriteengine on
Rewritecond%{http_host} ^www.jb51.net
Rewritecond%{request_uri}!^user.php$
Rewritecond%{request_uri}. php$
Rewriterule (. *). php$ http://www.jb51.net/$1/[R]
Rewritecond%{http_host}!^www.jb51.net
Rewriterule ^ (. +)%{http_host} [C]
Rewriterule ^ ([^.] +). Jb51.net http://www.jb51.net/user.php?username=$1
Example two:
Copy Code code as follows:
/type.php?typeid=*–>/type*.html
/type.php?typeid=*&page=*–>/type*page*.html
Rewriterule ^/type ([0-9]+). html$/type.php?typeid=$1 [PT]
Rewriterule ^/type ([0-9]+) page ([0-9]+). html$/type.php?typeid=$1&page=$2 [PT]