Apache Rewrite rules detailed

Source: Internet
Author: User

Reference Link: http://slj.me/2009/04/apache-rewrite-regular/

Introduction to 1.Rewrite Rules

The main function of Rewirte 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 you install the rewrite module directly, one is to compile 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 based on server-level (httpd.conf) , one is to use rewriteengine on to open rewrite function in httpd.conf Global, and the other is to use rewriteengine in local On to open the rewrite function, as illustrated below, it is important to note that the rewrite function must be opened 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 (option in httpd.conf FollowSymLinks) and in. htaccess to declare rewriteengine on.

2. The meaning of markup in Apache mod_rewrite rules

1 1) r[=Code] (force redirect) forces external redirection 2 to force an alternate string plus http://thishost[:thisport]/The prefix is redirected to an external URL. If code is not specified, the default 302 HTTP status code will be used. 3 2F (Force URL to is forbidden) disables the URL and returns the 403HTTP status code. 4 3G (force URL to is gone) forces the URL to gone and returns the 410HTTP status code. 5 4) P (Force proxy) enforces the use of proxy forwarding. 6 5) L (last rule) indicates that the current rule is the final rule, stopping the rewrite of the rule after parsing. 7 6) N (next round) re-run the rewrite process starting with the first rule. 8 7) C (chained with next rule) is associated with the next one 9 if the rule match is handled normally, the flag is invalid, and if it does not match, all the associated rules below are skipped. 8) t=mime-type (force MIME type) enforces MIME type 9) NS (used only if no internal sub-request) is used only for internal sub-requests.NC (no case)QSA (query string append) Append request string (No.) NE (no URI escaping of output) does not escape special characters in output 15 for example: Rewri terule/foo/(. *)/bar?arg=p1%3d$1 [R,ne] will correctly convert/foo/zoo to/bar?arg=p1=zoo16) PT (pass through to next Handler) passed to the next processing, for example: Rewriterule ^/abc (. *)/def$1 [PT] # will be handed to/def rule processing alias/def/ghi20) s=num (sk IP Next rule (s)) Skip Num rule e=var:val (SET environment variable) set environment variable       

3. Illustrative examples

Example I. The following is a rule defined in a virtual host. The function is to put the client request host prefix not www.kiya.cn and 70.40.213.183 jump to the host prefix to http://www.kiya.cn, avoid the same content of the page has multiple points to the domain name, such as http://kiya.cn.

1Namevirtualhost 70.40.213.183:802 ServerAdmin [email protected]3DocumentRoot "/Web "4 ServerName kiya.cn5Rewriteengine on#turn on the Rewirte feature6Rewritecond%{http_host}!^www.kiya.cn [NC]#The prefix in the host that declares the client request is not a www.kiya.cn, where [NC] means ignoring the case7Rewritecond%{http_host}!^70.40.213.183 [NC]#The prefix in the host that declares the client request is not a 70.40.213.183, where [NC] means ignoring the case8Rewritecond%{http_host}!^$#The prefix in the host that declares the client request is not empty9Rewriterule ^ (. *) http://www.kiya.cn/[L]#The implication is that if the prefix in the host requested by the client meets the above criteria, jumping directly to http://www.kiya.cn/,[l] means that the rewrite operation is stopped immediately and no additional rewrite rules are applied. Here's the. * Means matching all URLs that do not contain newline characters, () The function of parentheses is to make a mark for all characters so that they can be used later. refers to the (. *) character in front. 

Example two: Jump to www.sicasoft.com when entering a en.sicasoft.com domain name

1 Rewriteengine on 2 rewritecond%{http_host} ^en.sicasoft.com [NC]3 rewriterule ^ (. *)/HTTP/ www.sicasoft.com/[L]

Third, the game Card software recently changed the domain name, the new domain name for www.sicasoft.com, more brief and good remember. At this time need to the original domain name ss.kiya.cn, as well as the Forum address ss.kiya.cn/bbs/directed to the new domain name, so that users can find, and so that the original forum URL continues to be valid without appearing 404 Not found, such as the original http://ss.kiya.cn/ Bbs/tread-60.html, let it continue in effect under the new domain name, click Forward to Http://bbs.sicasoft.com/tread-60.html, and other pages, such as the original http://ss.kiya.cn/ Purchase will not go to the level two domain name bbs.sicasoft.com/purchase, but to Www.sicasoft.com/purchase
Following such a requirement redirection rules should be written like this:

1 Rewriteengine on 2 Rewritecond%{request_uri} ^/bbs/3 rewriterule ^bbs/(. *) http://bbs.sicasoft.com/$1 [r=  PERMANENT,L]4 rewritecond%{request_uri}!^/bbs/5 rewriterule ^ (. *) http://www.sicasoft.com/$ 1 [r=permanent,l]

Example Iv. achieve the following two requirements at the same time:
1. Use http://www.zzz.com/xxx.php to access http://www.zzz.com/xxx/
2. Use http://yyy.zzz.com to access the functions of http://www.zzz.com/user.php?username=yyy

1 Rewriteengine on 2 rewritecond%{http_host} ^www.zzz.com3 rewritecond%{request_uri}!^user.php$ 4 rewritecond%{Request_uri}. php$5 rewriterule (. *). php$ http://www.zzz.com/$1/ [R]  6 rewritecond%{http_host}!^www.zzz.com7 rewriterule ^ (. +)%{Http_host} [ C]8 rewriterule ^ ([^.] +). zzz.com http://www.zzz.com/user.php?username=$1

Example Five.

/type.php?typeid=*–>/type*.html
/type.php?typeid=*&page=*–>/type*page*.html

1 rewriterule ^/type ([0-9]+). html$/type.php?typeid=$1 [PT]2 rewriterule ^/type ([0-9]+) Page ([0-9]+). html$/type.php?typeid=$1&page=$2 [PT]

Example six: Configuring a multiuser Virtual server using Apache URL rewrite

To implement this function, first to open the DNS server on the domain name of the pan domain name resolution (do it yourself or find a domain Name Service provider do). For example, I have *.kiya.us and *.kiya.cn all resolved to my IP address 70.40.213.183.

Then, take a look at the settings of my Apache virtual host for *.kiya.us.

1 ServerAdmin [email protected]2documentroot/home/www/www.kiya.us3 ServerName dns.kiya.us4Serveralias dns.kiya.us kiya.us *. kiya.us5customlog/var/log/httpd/osa/Access_log.log "Common6errorlog/var/log/httpd/osa/Error_log.log "7 allowoverride None8 Order Deny,allow9 #Adddefaultcharset GB2312Ten Rewriteengine on OneRewritecond%{http_host} ^[^.] +.kiya. (cn|us) $ ARewriterule ^ (. +)%{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 both *.kiya.cn and *.kiya.us to/home/www/www.kiya.us

Keep looking, I've configured the URL rewrite rule here.

1Rewriteengine on#open URL rewrite feature2Rewritecond%{http_host} ^[^.] +.kiya. (Cn|us) $#match Criteria If the user enters a URL in which the hostname is similar to xxxx.kiya.us or xxxx.kiya.cn executes the following sentence3Rewriterule ^ (. +)%{http_host}$1 [C]#Pass the user into the complete address (except for the get mode parameter) as an argument to the next rule, [C] is the meaning of the next rule in series chain4Rewriterule ^ ([^.] +). Kiya. (Cn|us) (. *) $/home/www/dev.kiya.us/sylvan$3?un=$1&%{query_string} [L]5 #The most critical is this sentence, the use of the certificate expression to resolve the user input URL address, the host name of the user name information as a parameter named UN passed to the/home/www/dev.kiya.us directory 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 indicated 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 the new address, while using an internal jump the address in the browser does not change and looks more like the actual two-level domain name Virtual server. 

Reboot the Apache server after Setup and you're done!

Example Seven. Rewrite anti-theft chain regular, not allow www.im286.com www.chinaz.com these two sites hotlinking, other sites can hotlinking.

1 Rewriteengine on 2 rewritecond%{http_referer} chinaz.com [NC]3 rewritecond%{http_referer} im286.com [ NC]4 rewriterule. *\. (jpg|jpeg|gif|png|rar|zip|txt|ace|torrent|gz|swf) $ http://www.xxx.com/fuck.png [r,nc,l]

Apache Rewrite rules detailed

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.