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.
There are two methods for server-level (httpd.conf), one is to use rewriteengine on to open the rewrite function under the global httpd.conf, and the other is to use Rewriteengine in the local area. On to turn on the rewrite feature, the following will illustrate the need to note that the rewrite feature must be turned on with Rewriteengine on in each virtualhost. Otherwise, there is no rewriteengine on the VirtualHost and the rules in it will not take effect.
Based on the directory level (. htaccess), it is important to note that the FollowSymLinks property of this directory must be opened and in. htaccess to declare rewriteengine on.
2, the example explains:
Example one. The following are the rules defined in a virtual host. function is to the client requested host prefix is not www.jb51.net and 70.40.213.183 to jump to the host prefix for http://www.jb51.net, avoid the same content of the page has more than one point to the domain name, such as Http://jb51.net.
Copy Code code as follows:
Namevirtualhost 70.40.213.183:80
ServerAdmin slj@jb51.net
DocumentRoot "/web"
ServerName jb51.net
Rewriteengine on #打开rewirte功能
Rewritecond%{http_host}!^www.jb51.net [NC] #声明Client请求的主机中前缀不是www. jb51.net, where [NC] means ignoring case
Rewritecond%{http_host}!^70.40.213.183 [NC] #声明Client请求的主机中前缀不是70.40.213.183, where [NC] means ignoring case
Rewritecond%{http_host}!^$ #声明Client请求的主机中前缀不为空
Rewriterule ^ (. *) http://www.jb51.net/[L] #含义是如果Client请求的主机中的前缀符合上述条件, direct jump to http://www.jb51.net/,[l] means immediately stop the rewrite operation , and no more overriding rules are applied. Here's. * Refers to matching all URLs that do not contain newline characters, the function of () parentheses is to make all the characters a mark so that they can be used later. is to refer to the (. *) character in front.
Example two. Jump to Www.jb51.net when you enter a en.jb51.net domain name
Copy Code code as follows:
Rewriteengine on
Rewritecond%{http_host} ^en.jb51.net [NC]
Rewriterule ^ (. *) http://www.jb51.net/[L]
Third, the game Card software recently replaced the domain name, the new domain name for www.jb51.net, more brief good remember. At this time need to the original domain name Ss.jb51.net, as well as the Forum address ss.jb51.net/bbs/directed to the new domain name, so that users can find, and the original forum URL to continue to be effective without 404 Not found, such as the original http:// Ss.jb51.net/bbs/tread-60.html, let it continue to be valid under the new domain name, click and forward to http://bbs.jb51.net/tread-60.html, while other pages, such as the original http:// Ss.jb51.net/purchase does not go to level two domain bbs.jb51.net/purchase, but to Www.jb51.net/purchase
Following this requirement redirection rules should be written like this:
Copy Code code as follows:
Rewriteengine on
Rewritecond%{request_uri} ^/bbs/
Rewriterule ^bbs/(. *) http://bbs.jb51.net/$1 [r=permanent,l]
Rewritecond%{request_uri}!^/bbs/
Rewriterule ^ (. *) http://www.jb51.net/$1 [r=permanent,l]
3.Apache mod_rewrite rule Rewrite logo list
Copy Code code as follows:
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.
Copy Code code as follows:
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:
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
4.Apache Rewrite example set
URL redirection
Example one:
At the same time meet the following two requirements:
1. Access to http://www.zzz.com/xxx/with http://www.zzz.com/xxx.php
2. Use http://yyy.zzz.com to access the HTTP://WWW.ZZZ.COM/USER.PHP?USERNAME=YYY function
Copy Code code as follows:
Rewriteengine on
Rewritecond%{http_host} ^www.zzz.com
Rewritecond%{request_uri}!^user.php$
Rewritecond%{request_uri}. php$
Rewriterule (. *). php$ http://www.zzz.com/$1/[R]
Rewritecond%{http_host}!^www.zzz.com
Rewriterule ^ (. +)%{http_host} [C]
Rewriterule ^ ([^.] +). zzz.com http://www.zzz.com/user.php?username=$1
Example two:
/type.php?typeid=*–>/type*.html
/type.php?typeid=*&page=*–>/type*page*.html
Copy Code code as follows:
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]
5. Configure a multiuser virtual server using Apache URL rewrite
To implement this feature, you first need to open the domain name resolution on the DNS server (do it yourself or find a domain Name Service provider). For example, I have *.kiya.us and *.jb51.net all resolved to my IP address 70.40.213.183.
Then, take a look at the setup of my Apache virtual host for *.kiya.us.
Copy Code code as follows:
ServerAdmin webmaster@kiya.us
Documentroot/home/www/www.kiya.us
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
#AddDefaultCharset GB2312
Rewriteengine on
Rewritecond%{http_host} ^[^.] +.kiya. (Cn|us) $
Rewriterule ^ (. +)%{http_host}$1 [C]
Rewriterule ^ ([^.] +). Kiya. (Cn|us) (. *) $/home/www/www.kiya.us/sylvan$3?un=$1&%{query_string} [L]
In this setting, I set the document root of *.jb51.net and *.kiya.us to/home/www/www.kiya.us
Continue to watch, here I have configured the URL rewrite rule.
Copy Code code as follows:
Rewriteengine on #打开URL rewrite function
Rewritecond%{http_host} ^[^.] +.kiya. (Cn|us) $ #匹配条件, if the user enters a URL in which the host name is similar to xxxx.kiya.us or xxxx.jb51.net executes the following sentence
Rewriterule ^ (. +)%{http_host}$1 [C] #把用户输入完整的地址 (except for the parameters of the Get method) is passed as an argument to the next rule, [C] is chain the next rule in concatenation
Rewriterule ^ ([^.] +). Kiya. (Cn|us) (. *) $/home/www/dev.kiya.us/sylvan$3?un=$1&%{query_string} [L]
# The key is this sentence, the use of the card expression to resolve the user input URL address, the hostname of the username information as a parameter named UN to the/home/www/dev.kiya.us directory of the script, and followed by the user entered the Get method of the incoming parameters. and indicate that this is the last rule ([L] rule). Note that the rewritten address specified in this sentence is the absolute path on the server, which is an internal jump. If you use a URL format such as http://xxxx, it is called an external jump. Using an external jump, the URL address in the browser will change to a new address, while using internal jumps the address in the browser does not change and looks more like the actual level two domain name virtual server.
Reboot the Apache server after Setup is done!
Update May 1, 2009
Today on the Internet to see someone ask a question:
Seek rewrite anti-theft chain regular
Do not allow www.im286.com www.chinaz.com these two sites hotlinking, other sites can hotlinking rules how to write.
The answer in the forum is:
Copy Code code as follows:
Rewriteengine on
Rewritecond%{http_referer} chinaz.com [NC]
Rewritecond%{http_referer} im286.com [NC]
Rewriterule. *\. (jpg|jpeg|gif|png|rar|zip|txt|ace|torrent|gz|swf) $ yun_qi_img/fuck.png [r,nc,l]
Update May 7, 2009
Introduction of an article: http://lamp.linux.gov.cn/Apache/ApacheMenu/mod/mod_rewrite.html
Update May 24, 2009
First, about whether you need to use full escape, such as in Rewritecond%{http_referer} chinaz.com [NC] to convert chinaz.com to chinaz\.com
The answer is that both are possible.
Two, today in doing yourcaddy.com (that is, I did last year Planetcoachella deformation), in the host can not turn the normal, and then found the problem:
On Hostmonster and on my own machine, it's used
Rewriterule ^business/([^\.] +) $ biz/detail.php?name=$1 [L]
Reach the rewrite. And in the GoDaddy host, is this:
Rewriterule ^business/([^\.] +) $/biz/detail.php?name=$1 [L]
One more before the target file
Now think about it, it may be because there is no designated Rewritebase, as to whether it is not a day I verify.
Third, add two examples of the USER AGENT and automatically Add. php extensions and auto-swap. html to. php Extensions:
1
Rewriteengine on
Rewritecond%{http_user_agent} ^msie [Nc,or]
Rewritecond%{http_user_agent} ^opera [NC]
Rewriterule ^.*–[f,l] Here "-" means no replacement, browsers for IE and opera visitors will be barred from accessing.
2
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
Restricted directories can only display pictures
< Ifmodule mod_rewrite.c>
Rewriteengine on
Rewritecond%{request_filename}!^.*\. (gif|jpg|jpeg|png|swf) $
Rewriterule. *$–[f,l]
</ifmodule>
Update June 10, 2009
Supplemental, an override of a specific file extension.
To override a file with some extensions:
Rewriterule (. *.css$|. *.js$) gzip.php?$1 [L]
If you want to exclude some extensions:
Rewriterule!\. (Js|ico|gif|jpg| Jpg|png| PNG|CSS|PDF|SWF) $ index.php