Apache Url_rewrite Routing Rewrite

Source: Internet
Author: User
Tags webhosting
1, RewriteIntroduction to 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 Server-Level (httpd.conf) and directory-level (. htaccess) Two ways. If you want to use the rewrite module, you must first install or load the rewrite module. There are two types of methods that are compiled ApacheWhen 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's no rewriteengine on it in VirtualHost. ruleswill 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:
The following are the rules defined in a virtual host. The function is to move the host prefix of the client request not www.colorme.com and 203.81.23.202 to the host prefix for http://www.colorme.com.cn, avoiding when the user writes to the address bar http:// Colorme.com.cn can not log in as a member of the site.
Namevirtualhost 192.168.100.8:80
<virtualhost 192.168.100.8:80>
ServerAdmin webmaster@colorme.com.cn
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 case
Rewritecond%{http_host}!^203.81.23.202 [NC] #声明Client请求的主机中前缀不是203.81.23.202,[nc] means ignoring case
Rewritecond%{http_host}!^$ #声明Client请求的主机中前缀不为空, [NC] means ignoring case
Rewriterule ^/(. *) http://www.colorme.com.cn/[l]# meaning is to jump directly to http://www.colorme.com.cn/if the prefix in the host of the client request meets the above criteria, [ L] means that the rewrite operation is stopped immediately and no other overriding rule is 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.
</VirtualHost>


Example two. Jump to profile.test.com when you enter a folio.test.com domain name
Listen 8080
Namevirtualhost 10.122.89.106:8080
<virtualhost 10.122.89.106:8080>
ServerAdmin webmaster@colorme.com.cn
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]
</VirtualHost>


3.Apache mod_rewrite rule Rewrite logo 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=zed
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 collectionForward a domain name to another domain name in httpd
Virtual host world recently changed domain name, new domain name for www.wbhw.com, more brief good 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 the original forum URL continues to be valid without 404 found, Like the original http://www. Webhosting-world.com/forums/-f60.html, let it continue to be effective under the new domain name, click and forward to http://bbs.wbhw.com/-f60.html, which needs to use Apache Mod_rewrite function to implement.

Add the following redirection rule in < virtualhost>:

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 of the contents are as follows:
< VirtualHost *:80>
Serveralias webhosting-world.com
ServerAdmin admin@webhosting-world.com
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]
</virtualhost>



URL Redirection Example One:
1.http://www.zzz.com/xxx.php-> http://www.zzz.com/xxx/
The function of 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/[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

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 would have *.semcase.com and *.semcase.cn all resolved to my LinuxOn the server.

Then, take a look at the setup of my Apache virtual host for *.semcase.com.

#*.com,*.osall.net
<virtualhost *:80>
ServerAdmin webmaster@semcase.com
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 "
<Directory/home/www/www.semcase.com>
AllowOverride None
Order Deny,allow
#AddDefaultCharset GB2312
</Directory>
<ifmodule mod_rewrite.c>
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] </IfModule>
</VirtualHost>


In this setting, I set the document root of *.semcase.net and *.semcase.com to/home/www/www.semcase.com
But, continue to look down, see <ifmodule mod_rewrite.c>...</ifmodule> configuration. Here I have configured the URL rewrite rule.


Rewriteengine on #打开URL rewrite function
Rewritecond%{http_host} ^[^.] +.osall. (com|net) $ #匹配条件, if the user enters a URL in which the host name is similar to xxxx.semcase.com or xxxx.semcase.cn 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 ^ ([^.] +). Osall. (com|net) (. *) $/home/www/dev.semcase.com/sylvan$3?un=$1&%{query_string} [L] # The most critical is this sentence, the use of the card expression parsing user input URL address, The user name information in the hostname is passed to the script in the/home/www/dev.semcase.com directory as a parameter named UN, followed by the incoming parameter of the Get method entered by the user. 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.

After this setup, restart the Apache server, test it, it is done.

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.