Parameters related to rewrite of Apache

Source: Internet
Author: User
Parameters related to rewrite of Apache

Overview of Apache mod_rewrite rule rewriting flag

R [= Code] (Force redirect) force external redirection
Force the replacement string to add http: // thishost [: thisport]/prefix to the external URL. If the code is not specified, the default 302 HTTP status code will be used.
F (Force URL to be forbidden) disables the URL and returns a 403http status code.
G (Force URL to be gone) if the force URL is gone, the response HTTP status code is returned.
P (Force proxy) force use of proxy forwarding.
L (Last rule) indicates that the current rule is the last rule, and the rule is overwritten after the analysis is stopped.
N (Next round) re-run the rewrite process from the first rule.
C (Chained with next rule) is associated with the next rule
If the rule matches, it is processed normally and the flag is invalid. If the rule does not match, all associated rules are skipped.
T = Mime-type (Force MIME type) force MIME type
NS (Used only if no internal sub-Request) is only used for internal subrequests
NC (No case) case insensitive
QSA (Query string append) append request string
Ne (No URI escaping of output) not escape special characters from the output
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 the next Processing
For example:
Rewriterule ^/ABC (. *)/DEF $1 [pt] # will be handed over to/DEF rules for processing
Alias/DEF/Ghi
S = Num (skip next rule (s) skips num rules
E = Var: Val (set environment variable) sets the environment variable

Common server variables used by mod_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 rewriterule rule expression:
. Match any single character
[Chars] matched string: chars
[^ Chars] unmatched string: chars
Text1 | string that can be selected by text2: text1 or text2
? Matches 0 to 1 Characters
* Matches 0 to multiple characters
+ Match 1 to multiple characters
^ String start flag
$ String end flag
\ N Escape Character Mark

Reverse reference $ n is used to call the matching variable in rewriterule (0 <= n <= 9)
Reverse reference % N is used to call the last matched variable in rewritecond (1 <= n <= 9)

Applicable identifier of rewritecond
'Nocase | NC '(no case) Ignore size
'Ornext | or' (or next condition) logic or, multiple rewritecond conditions can be matched simultaneously

Applicable identifier of the rewriterule
'Redirect | r [= Code] '(Force redirect) force rewrite to an external redirection starting with HTTP (note URL changes) such as: [R = 301, l]
'Forbidden | f' (Force URL to be forbidden) is rewritten to prohibit access.
'Proxy | P' (Force proxy) is rewritten to the HTTP path accessed through the proxy
'Last | l' (last rule) indicates the last rewrite rule flag. If it matches, it will not be executed.
'Next | n' (next round) loops through the same rule until it cannot match
'Chain | C' (chained with next rule) If this rule is matched, the following chain-marked rule is continued.
'Type | T = mime-type' (Force MIME type) specifies the MIME type
'Nosubreq | ns' (used only if no internal sub-request)
'Nocase | NC '(no case) Ignore size
'Qsappend | qsa' (query string append) append the query string.
'Noescape | ne' (no URI escaping of output) disables characters in the URL from being automatically converted to % [0-9] +.
'Passthrough through to next handler 'applies the rewriting result to mod_alias
'Skip | S = num' (skip next rule (s) skips the following rules
'Env | E = var: Val '(set environment variable) to add Environment Variables

Practice
Example:
Rewriteengine on
Rewritecond % {http_user_agent} ^ MSIE [NC, or]
Rewritecond % {http_user_agent} ^ opera [Nc]
Rewriterule ^. *-[F, L] Here "-" indicates no replacement. Visitors with IE and opera browsers will be banned from accessing the site.

Example:
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 images

rewriteengine on
rewritecond % {request_filename }! ^. *\. (GIF | JPG | JPEG | PNG | SWF) $
rewriterule. * $-[F, L]

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.