Apache rewrite rule details [go]

Source: Internet
Author: User
Tags webhosting

1, rewrite rules introduction:
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 for server-level-based (httpd.conf)

One is to use the Rewriteengineon to open the rewrite function directly under the global httpd.conf;

The other is in the local use of Rewriteengineon to open the rewrite function, the following will illustrate, it is important to note that you must use Rewriteengineon in each virtualhost to open the rewrite function. Otherwise there is no rewriteengine on the VirtualHost and the rules in it will not take effect.

Based on the directory-level (. htaccess), one thing to note is that you must open the FollowSymLinks property of this directory and declare rewriteengine on in the. htaccess.


Here are the rules defined in a virtual host. The function is to put the client request host prefix not www.colorme.com and 203.81.23.202 both jump to the host prefix to http://www.colorme.com.cn, avoid when the user writes in the Address bar/HTTP// Colorme.com.cn cannot log on to the website as a member.
Namevirtualhost 192.168.100.8:80
ServerAdmin [email protected]
DocumentRoot "/web/webapp"
ServerName www.colorme.com.cn
ServerName colorme.com.cn
Rewriteengine on #打开rewirte功能
Rewritecond%{http_host}!^www.colorme.com.cn [NC] #声明Client请求的主机中前缀不是www. Colorme.com.cn,[nc] means ignoring the case
Rewritecond%{http_host}!^203.81.23.202 [NC] #声明Client请求的主机中前缀不是203.81.23.202,[nc] means ignoring the case
Rewritecond%{http_host}!^$ #声明Client请求的主机中前缀不为空, [NC] means ignoring case
Rewriterule ^/(. *) http://www.colorme.com.cn/[L]
#含义是如果Client请求的主机中的前缀符合上述条件, jumping directly to http://www.colorme.com.cn/,[l] means that the rewrite operation is stopped immediately and no other 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 profile.test.com when entering a folio.test.com domain name
Listen 8080
Namevirtualhost 10.122.89.106:8080
ServerAdmin[email protected]
DocumentRoot "/usr/local/www/apache22/data1/"
ServerName profile.test.com
Rewriteengine on
Rewritecond%{http_host} ^folio.test.com [NC]
Rewriterule ^/(. *) http://profile.test.com/[L]
3.Apache mod_rewrite Rules rewrite flags at a glance
1) R[=code] (force redirect) forced external redirection
Forces a http://thishost[:thisport]/prefix to be redirected to an external URL in an alternate string. If code is not specified, the default 302 HTTP status code will be used.
2) F (force URL to is forbidden) disables the URL and returns the 403HTTP status code.
3) G (force URL to is gone) forces the URL to gone and returns the 410HTTP status code.
4) P (force proxy) enforces the use of proxy forwarding.
5) L (last rule) indicates that the current rule is the final rule, stopping the rewrite of the rule after parsing.
6) N (next round) re-run the rewrite process starting with the first rule.
7) C (chained with next rule) is associated with the next law
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) enforce MIME type
9) NS (used only if no internal sub-request) is only used for internal sub-requests
NC (no case) is not casing-sensitive
One) QSA (query string append) Append request string
NE (no URI escaping of output) does not escape special characters in the output
For example: rewriterule/foo/(. *)/bar?arg=p1\%3d$1 [R,ne] will be able to correctly convert/foo/zoo to/bar?arg=p1=zed
PT (pass through to next handler) is passed to the next processing
For example:
Rewriterule ^/abc (. *)/def$1 [PT] # will be handed over to/def rule processing
Alias/def/ghi
S=num (skip next rule (S)) skips num rule
E=var:val (Set environment variable) set environment variables
4.Apache Rewrite Example collection
Forward a domain name to another domain name in httpd the world has recently changed its domain name, and the new domain name is www.wbhw.com, which is more brief and easy to remember. At this time need to the original domain name webhosting-world.com, as well as the Forum address webhosting-world.com/forums/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, For example, the original http://www.webhosting-world.com/forums/-f60.html, let it continue to be valid under the new domain name, click Forward to the http://bbs.wbhw.com/-f60.html, which need to use Apache's mod_rewrite feature to implement.
Add the following redirection rule in:
Rewriteengine on
# Redirect Webhosting-world.com/forums to Bbs.wbhw.com
Rewritecond%{request_uri} ^/forums/
rewriterule/forums/(. *) http://bbs.wbhw.com/$1 [r=permanent,l]
# Redirect webhosting-world.com to Wbhw.com
Rewritecond%{request_uri}!^/forums/
Rewriterule/(. *) http://www.wbhw.com/$1 [r=permanent,l]
After adding the above rules, all the contents are as follows:
Serveralias webhosting-world.com
ServerAdmin[email protected]
Documentroot/path/to/webhosting-world/root
ServerName www.webhosting-world.com
Rewriteengine on
# Redirect Webhosting-world.com/forums to Bbs.wbhw.com
Rewritecond%{request_uri} ^/forums/
rewriterule/forums/(. *) http://bbs.wbhw.com/$1 [r=permanent,l]
# Redirect webhosting-world.com to Wbhw.com
Rewritecond%{request_uri}!^/forums/
Rewriterule/(. *) http://www.wbhw.com/$1 [r=permanent,l]
URL redirection
Example one:
1.http://www.zzz.com/xxx.php-> http://www.zzz.com/xxx/
Features of the 2.http://yyy.zzz.com-> http://www.zzz.com/user.php?username=yyy
Rewriteengine on
Rewritecond%{http_host} ^www.zzz.com
Rewritecond%{request_uri}!^user\.php$
Rewritecond%{request_uri} \.php$
Rewriterule (. *) \.php$ Http://www.zzz.com/$1/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
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 multi-user 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've parsed *.semcase.com and *.semcase.cn all over my linux server.
Then, take a look at the settings of my Apache virtual host for *.semcase.com.
#*.com,*.osall.net
ServerAdmin[email protected]
Documentroot/home/www/www.semcase.com
ServerName dns.semcase.com
Serveralias dns.semcase.com semcase.com semcase.net *.semcase.com *.semcase.net
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} ^[^.] +\.osall\. (com|net) $
Rewriterule ^ (. +)%{http_host}$1 [C]
Rewriterule ^ ([^.] +) \.osall\. (com|net) (.*)$
/home/www/www.semcase.com/sylvan$3?un=$1&%{query_string} [L]
In this setting, I set the document root of both *.semcase.net and *.semcase.com to/home/www/www.semcase.com
But, keep looking and see ... Are you configured? Here I configure the URL rewrite rule.
Rewriteengine on #打开URL rewrite function
Rewritecond%{http_host} ^[^.] +.osall. (com|net) $ #匹配条件 If the user enters a URL in which the hostname is similar to xxxx.semcase.com or xxxx.semcase.cn executes the following sentence
Rewriterule ^ (. +)%{http_host}$1 [C] #把用户输入完整的地址 (except for parameters of Get mode) passed as a parameter to the next rule, [C] is the meaning of the next rule in series chain
Rewriterule ^ ([^.] +). Osall. (com|net) (. *) $/home/www/dev.semcase.com/sylvan$3?un=$1&%{query_string} [L]
#最关键的是这一句, use the certificate expression to parse the URL address entered by the user, pass the username information in the host name as a parameter named UN to the script in the/home/www/dev.semcase.com directory, and follow the incoming parameters of the Get method entered by the user. 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.
After this setup, restart the Apache server, test it, and you're done!

Apache rewrite rule details [go]

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.