Apache's rewrite rewrite is very common and is now summed up.
Apache mod_rewrite Rules rewrite flags at a glance
R[=code] (Force redirect) forcing 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 will be used.
F(Force URL to is forbidden) disables the URL and returns the 403HTTP status code.
G(Force URL to is gone) forces the URL to gone and returns the 410HTTP status code.
P(Force proxy) forces the use of proxy forwarding.
L(last rule) indicates that the current rule is the final rule, stopping the rewrite of the rule after parsing.
N(Next round) re-runs the rewrite process starting with the first rule.
C(chained with Next rule)
If the rule match is handled normally, the flag is invalid and if it does not match, all the associated rules below are skipped.
T=mime-type (Force MIME type) coercion MIME type
NS(Used only if no internal sub-request) is used only for internal sub-requests
NC(no case) insensitive
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=zed
PT(pass through to next handler) passes 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) setting environment variables
Server variables commonly used when using 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] Match string: chars
[^chars] mismatch 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 used for matching variable calls in Rewriterule (0 <= N <= 9)
Reverse reference%N for the last matching variable call in Rewritecond (1 <= N <= 9)
Rewritecond Applicable Markers
' Nocase| NC ' (no case) ignore size
' Ornext| or ' (or next condition) logical OR, can match multiple rewritecond conditions at the same time
rewriterule applicable marker
' redirect| R [=code] ' (Force redirect) forced override for external steering based on HTTP (note URL changes) such as: [R=301,l]
' forbidden| F ' (Force URL to is forbidden) override to disable access to
' proxy| P ' (force proxy) overrides the HTTP path to access via proxy
' last| L ' (last rule) final rewrite rule flag, if matched, no longer executes subsequent rules
' next| N ' (next round) loops the same rule until the matching
' chain| is not met 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 the MIME type
' nosubreq| NS ' (used only if no internal sub-request) skips
' nocase| if it is an internal sub-request NC ' (no case) ignores size
' qsappend| QSA ' (query string append) append query string
' noescape| NE ' (no URI escaping of output) prohibits characters in the URL from being automatically escaped into%[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 several rules
' env| E=var:val ' (Set environment variable) add environment variable
Actual combat
Example:
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.
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 a catalog to display only pictures
< Ifmodule mod_rewrite.c>
Rewriteengine on
Rewritecond%{request_filename}!^.*. (gif|jpg|jpeg|png|swf) $
Rewriterule. *$–[f,l]
</ifmodule>
Apache Rewrite rules detailed