Apache rewrite implements URL jump and domain jump

Source: Internet
Author: User

1, Rewirte main function is to implement the URL of the jump, 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}!^$ # The prefix in the host that declares the client request is not NULL
Rewriterule ^ (. *)  http://www.kiya.cn/  [l] #含义是如果Client请求的主机中的前缀符合上述条件, jump directly to http:// www.kiya.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 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]

http://ss.kiya.cn/bbs/tread-60.html , let it continue in effect under the new domain name, click Forward to , while other web pages, such as the original http://ss.kiya.cn/purchase will not go to level two domain bbs.sicasoft.com/ Purchase, but to  www.sicasoft.com/purchase
The redirect rule should be written as follows:

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 is 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] # 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 chain 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 certificate expression to resolve the user input URL address, The username information in the hostname is passed to the script in the/home/www/dev.kiya.us directory as a parameter named UN, followed by 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.

Reboot the Apache server after Setup and you're done!
Update May 1,

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,

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

Update May,

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,

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

Apache rewrite implements URL jump and domain jump

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.