Apache rewrite implements URL jump and domain jump

Source: Internet
Author: User

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), one thing to note is that you must open the FollowSymLinks property of this directory and declare rewriteengine on in the. htaccess.

2. 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.domain.cn and 202.91.246.20 jump to the host prefix to http://www.domain.cn, avoid the same content of the page has multiple points to the domain name, such as http://domain.cn.

Namevirtualhost 202.91.246.20:80
ServerAdmin [email protected]
DocumentRoot "/web"
ServerName domain.cn

Rewriteengine on #打开rewirte功能
Rewritecond%{http_host}!^www.domain.cn [NC] #声明Client请求的主机中前缀不是www. domain.cn, where [NC] means ignoring case
Rewritecond%{http_host}!^202.91.246.20 [NC] #声明Client请求的主机中前缀不是202.91.246.20, where [NC] means ignoring case
Rewritecond%{http_host}!^$ #声明Client请求的主机中前缀不为空
Rewriterule ^ (. *) http://www.kiya.cn/[L] #含义是如果Client请求的主机中的前缀符合上述条件, jumping directly to http://www.kiya.cn/,[l] means stopping the rewrite operation 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 http:www.domain.cn when entering a example.com domain name

Rewriteengine on
Rewritecond%{http_host} ^example.com [NC]
Rewriterule ^ (. *) http://www.domain.cn/[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:

Rewriteengine on
Rewritecond%{request_uri} ^/bbs/
Rewriterule ^bbs/(. *) http://bbs.sicasoft.com/$1 [r=permanent,l]
Rewritecond%{request_uri}!^/bbs/
Rewriterule ^ (. *) http://www.sicasoft.com/$1 [r=permanent,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=zoo
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

URL redirection

Example one:
At the same time meet the following two requirements:
1. Use http://www.domain.cn/xxx.php to access http://www.domain.cn/xxx/
2. Use http://xiaoye.domain.cn to access the functions of Http://www.domain.cn/user.php?username=xiaoye

Rewriteengine on
Rewritecond%{http_host} ^www.domain.cn
Rewritecond%{request_uri}!^user.php$
Rewritecond%{request_uri}. php$
Rewriterule (. *). php$ http://www.domain.cn/$1/[R]
Rewritecond%{http_host}!^www.domain.cn
Rewriterule ^ (. +)%{http_host} [C]
Rewriterule ^ ([^.] +). domain.cn http://www.domain.cn/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 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.

ServerAdmin [email protected]
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 both *.kiya.cn and *.kiya.us to/home/www/www.kiya.us

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

Rewriteengine on #打开URL rewrite function
Rewritecond%{http_host} ^[^.] +.kiya. (cn|us) $ #匹配条件 If the user enters a URL in which the hostname is similar to xxxx.kiya.us or xxxx.kiya.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 ^ ([^.] +). Kiya. (Cn|us) (. *) $/home/www/dev.kiya.us/sylvan$3?un=$1&%{query_string} [L]
The most critical is this sentence, the use of the certificate expression to resolve the user input URL address, the hostname 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!
Update May 1, 2009

Today on the Internet to see someone ask a question:

Seeking 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:

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) $ http://www.xxx.com/fuck.png [r,nc,l]

Update May 7, 2009

Introduction of a post: http://lamp.linux.gov.cn/Apache/ApacheMenu/mod/mod_rewrite.html

Update May 24, 2009

First, about whether to use full escape, such as in Rewritecond%{http_referer} chinaz.com [NC] to change chinaz.com to chinaz\.com
The answer is, both are possible.

Second, today in doing yourcaddy.com (that is, I did last year Planetcoachella deformation), on the GoDaddy host can not turn normal, and later found the problem:
On Hostmonster and on my own machine,
Rewriterule ^business/([^\.] +) $ biz/detail.php?name=$1 [L]
Reach the rewrite. On the GoDaddy host, this is the case:
Rewriterule ^business/([^\.] +) $/biz/detail.php?name=$1 [L]
One more in front of the target file/
Now think about it, probably because I did not specify the rewritebase, as to whether or not I another time to verify.

Three, add two to judge the USER AGENT example and automatically Add. php extension and auto-replace. html to. php Extension Example:
1

Rewriteengine on
Rewritecond%{http_user_agent} ^msie [Nc,or]
Rewritecond%{http_user_agent} ^opera [NC]
Rewriterule ^.*–[f,l] Here "-" means no replacement, the browser for IE and opera visitors will be banned 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

Restrict a catalog to display only pictures
< Ifmodule mod_rewrite.c>
Rewriteengine on
Rewritecond%{request_filename}!^.*\. (gif|jpg|jpeg|png|swf) $
Rewriterule. *$–[f,l]
</ifmodule>

Update June 10, 2009

Supplemental, overrides for a specific file extension.

Rewrite files 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

Source: http://www.cnblogs.com/yeer/archive/2010/08/17/1801679.html

Apache rewrite implements URL jump and domain jump

Related Article

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.