The Apache Rewrite rule usage detailed explanation

Source: Internet
Author: User


So it is very important to understand the rewrite rule of Apache, which can implement pseudo static of URL. Or do 301 jump.

Rewrite logo

R[=code] (Force redirect) force external redirection
Force the substitution string plus
The http://thishost[:thisport]/prefix is redirected to an external URL. If code is not specified, the default 302 HTTP status code is used.
F (Force URL to be
Forbidden) disables the URL and returns a 403HTTP status code.
G (Force URL to IS gone)
Forces the URL to be gone and returns a 410HTTP status code.
P (force proxy) forces the use of proxy forwarding.
L (last Rule)
Indicates that the current rule is the last rule, and the override of the rule is stopped after parsing.
N (next round) again runs the rewrite process from the first rule.
C (Chained with
Next rule) is associated with the next one >
Such as
Fruit rule matching is normal processing, the flag is invalid, if does not match, then all the following associated rules are skipped.
T=mime-type (Force MIME type) force MIME type
NS (used only if
No internal sub-request) only for internal child requests
NC (no case) case-insensitive >
QSA (query string append) Append request string
NE (No URI
Escaping of output) does not escape special characters
For example: rewriterule/foo/(. *)/bar?arg=p1%3d$1 [R,ne]
will be able to correctly convert/foo/zoo into/bar?arg=p1=zed
PT (passes through to next handler) passed to the 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

Server variable when rewrite:

HTTP headers:http_user_agent, Http_referer, Http_cookie,http_host, http_accept
Connection & Request:remote_addr, query_string
Server Internals:document_root,server_port, Server_protocol
System Stuff:time_year, Time_mon, Time_day

Apache URL Rewrite configure multiuser virtual server

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'll put the *. Your web address. US and *. Your URL. cn all resolved to my IP addresses 70.40.213.183.

Then, take a look at my Apache about *. Your URL. US virtual host settings.

  code is as follows copy code

ServerAdmin webmaster@ your web address. US
Documentroot/home/www/www. Your web address. US
ServerName DNS. Your web address. US
Serveralias DNS. Your web address. US Your URL. US *. Your web address. 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} ^[^.] +. Your website. (cn|us) $
Rewriterule ^ (. +)%{http_host}$1 [C]
Rewriterule ^ ([^.] +). Your website. (Cn|us) (. *) $/home/www/www. Your web address. us/sylvan$3?un=$1&%{query_string} [L]

In this setting, I put *. Your web address, CN and *. The document root of us is set to/home/www/www. Your URL. us

Description of the rewrite rule expression:

. Match any single word character
[Chars]
Match string: Chars
[^chars]
Mismatched string: chars
Text1|text2
Selectable string: Text1 or Text2
?
Match 0 to 1 characters
* Match 0 to more characters
+ match 1 to more characters
^ String Start flag
$ string End Flag
N
Escape character Flag

Reverse reference $N variable invocation for matching in rewriterule (0 <= N <= 9)
Reverse reference%N for Rewritecond
The last match in the variable call (1 <= N <= 9)

Rewritecond Marker
' Nocase| NC ' (no case) ignores size
' Ornext| or ' (or next condition) logic or, you can match multiple rewritecond conditions at the same time

Rewriterule the applicable identifier
' Redirect| R [=code] ' (Force redirect) forced rewriting to an external turn based on HTTP opening (note change of URL)
such as: [R=301,l]


If you are using a Windows host, you should make a 301 turn setting in the Control Panel.

Of course, with 301 jump can also achieve all your URLs. com pages are all turned to www. your Web site. com page.
Create a file in the Web root directory. htaccess, and then write code similar to the following

The code is as follows Copy Code

Rewriteengine on
Rewritecond%{http_host} ^ your URL. com [NC]
Rewriterule ^ (. *) $ http://www. Your Web site. com/$1 [R=301,l]

The first line indicates open Urlrewrite
Line 23rd indicates that any link form behind your URL. com will be directed to www. Your URL. com corresponding links such as your URL. com/a/414.html will correspond to www. your website. com/a/414.html


' forbidden| F ' (Force
URL to is forbidden) overridden to prohibit access
' proxy| P ' (force proxy) overrides the HTTP path that is accessed through the proxy
' last| L ' (last rule) The final rewrite rule flag, if matched, no longer executes subsequent rules
' next| N ' (Next
Round) loops the same rule until the match cannot be satisfied
' chain| C ' (chained with next rule) if the rule is matched, continue with the following rules with the chain flag.
' type| T=mime-type ' (Force MIME
Type) specifies MIME type
' nosubreq| NS '
(used only if no internal sub-request) skips
' nocase| if it is an internal child request NC ' (no case) ignores size
' qsappend| QSA ' (query string append) additional query strings
' noescape| NE ' (no URI escaping of
output) prevents characters in a URL from automatically escaping into a%[0-9]+ form.
' passthrough| PT ' (pass through to next handler) applies the rewrite results to the Mod_alias
' skip| S=num ' (Skip next
Rule (s)) skips the following rules
' env| E=var:val ' (Set environment variable) add environment variable

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.