Apache Rewrite rule usage Instructions _linux

Source: Internet
Author: User

Rewrite logo
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.
F (force URL to is forbidden) disables the URL and returns a 403HTTP status code.
G (force URL to is gone) forces the URL to 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 final one and stops parsing the rule after it is overridden.
N (next round) again runs the rewrite process from the first rule.
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.
T=mime-type (Force MIME type) force MIME type
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) does not escape 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 (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 the 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

Description of the rewrite rule expression:
. Match any single word character
[chars] matching string: chars
[^chars] does not match 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 the last matching variable call in Rewritecond (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 overrides to an external turn based on HTTP (note URL changes) such as: [R=301,l]
' 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 is not met
' 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) skipped if it is an internal child request
' Nocase| NC ' (no case) ignores size
' Qsappend| QSA ' (query string append) append 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 Mod_alias
' Skip| S=num ' (skip next rule (s)) skip the following few rules
' Env| E=var:val ' (Set environment variable) add environment variables

Actual operation

Example:

Copy Code code as follows:

Rewriteengine on
Rewritecond%{http_user_agent} ^msie [Nc,or]
Rewritecond%{http_user_agent} ^opera [NC]
Rewriterule ^.*-[f,l] here "-" means no replacement, browsers for IE and opera visitors will be barred from accessing.

Example:

Copy Code code as follows:

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

Restricted directories can only display pictures

Copy Code code as follows:

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

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.