Instructions for apache rewrite rules

  • 2020-05-07 20:50:26
  • OfStack

rewrite logo
R[=code](force redirect) forces an external redirect

Forces http://thishost[:thisport]/ prefix to redirect to external URL. If code is not specified, the default 302 HTTP status code is used.
F(force URL to be forbidden) disables URL and returns 403HTTP status code.
G(force URL to be gone) forces URL to be GONE, returning 410HTTP status code.
P(force proxy) enforces the use of proxy forwarding.
L(last rule) indicates that the current rule is the last rule, and the rewrite of the rule after the analysis is stopped.
N(next round) again runs the rewrite process from rule 1.
C(chained with next rule) is associated with the next rule
This flag is invalid if the rule matches, and if it does not, then all the associated rules below are skipped.
T= MIME-type (force MIME type) enforces MIME type
NS (used only if no internal sub-request) is only used for non-internal sub-requests
NC(no case) is case insensitive
QSA(query string append) appends the request string
NE(no URI escaping of output) does not output escape special characters

For example: RewriteRule /foo/(.*) /bar? arg=P1\%3d$1 [R,NE] will correctly convert /foo/zoo to /bar? arg = P1 = zed

PT(pass through to next handler) is passed to the next process

Such as:
RewriteRule ^/abc(.*) /def$1 [PT] # will be handled by /def rule
Alias /def /ghi
S=num(skip next rule(s)) skips num rule
E=VAR:VAL(set environment variable) sets the environment variable

When rewrite server variable:
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 Rewrite regular expressions:
. Matches any single character
[chars] match string :chars
[^chars] mismatch string :chars
Optional string :text1 or text2
? Matches 0 to 1 characters
* matches 0 to more than one character
+ matches 1 to more than one character
^ string start flag
End of $string flag
\n escape symbol flag
The backreference $N is used for the matching variable call (0) in RewriteRule < = N < = 9)
The backreference %N is used for the last matched variable call in RewriteCond (1) < = N < = 9)

RewriteCond flag
'nocase|NC'(no case) ignore size
'ornext|OR' (or next condition) logic or, can match multiple RewriteCond conditions simultaneously
The appropriate identifier for RewriteRule
'redirect|R [=code]' (force redirect) is forcibly rewritten as an external steering based on the beginning of http (note the change in URL) as: [R=301,L]
'forbidden|F' (force URL to be forbidden) is rewritten to prohibit access
'proxy|P' (force proxy) is rewritten as the http path accessed through the proxy
'last|L' (last rule) last override rule flag, if matched, no later rules are executed
'next|N' (next round) loops with a rule until the match is not satisfied
'chain|C' (chained with next rule) continues with the following rule with the Chain flag if it matches the rule.
'type|T= MIME-type '(force MIME type) specifies the MIME type
'nosubreq|NS' (used only if no internal sub-request) is skipped if it is an internal subrequest
'nocase|NC' (no case) ignore size
'qsappend|QSA' (query string append) additional query string
'noescape|NE' (no URI escaping of output) does not allow the characters in URL to be automatically escaped as %[0-9]+.
'passthrough|PT' (pass through to next handler) applies the rewrite result to mod_alias
'skip|S=num' (skip next rule(s)) skips the following rules
'env|E=VAR:VAL' (set environment variable) add environment variables

actually operates

Example:


RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^MSIE [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Opera [NC]
RewriteRule ^.* - [F,L]  Here" - "Means no substitution, the browser is IE and Opera Visitors will be banned from visiting. 

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

Restrict the directory to images only


< IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^.*\.(gif|jpg|jpeg|png|swf)$
RewriteRule .*$  �  [F,L]
< /IfModule>


Related articles: