📄 mod_rewrite.html
字号:
rewriting process goes on until there are no more rules
unless explicitly terminated by a
<code><strong>L</strong></code> flag - see below.</p>
<p>There is a special substitution string named
'<code>-</code>' which means: <strong>NO
substitution</strong>! Sounds silly? No, it is useful to
provide rewriting rules which <strong>only</strong> match
some URLs but do no substitution, 例如,in
conjunction with the <strong>C</strong> (chain) flag to be
able to have more than one pattern to be applied before a
substitution occurs.</p>
<div class="note"><h3>Query String</h3>
<p><em>Pattern</em> will not match against the query string.
Instead, you must use a <code class="directive"><a href="#rewritecond">RewriteCond</a></code> with the
<code>%{QUERY_STRING}</code> variable. You can, however, create
URLs in the substitution string containing a query string
part. Just use a question mark inside the substitution string to
indicate that the following stuff should be re-injected into the
query string. When you want to erase an existing query string,
end the substitution string with just the question mark. To
combine a new query string with an old one, use the
<code>[QSA]</code> flag (see below).</p>
</div>
<div class="note"><h3>Substitution of Absolute URLs</h3>
<p>There is a special feature:
When you prefix a substitution field with
<code>http://</code><em>thishost</em>[<em>:thisport</em>]
then <strong>mod_rewrite</strong> automatically strips it
out. This auto-reduction on implicit external redirect
URLs is a useful and important feature when used in
combination with a mapping-function which generates the
hostname part. Have a look at the first example in the
example section below to understand this.</p>
<p><strong>Remember:</strong> An unconditional external
redirect to your own server will not work with the prefix
<code>http://thishost</code> because of this feature. To
achieve such a self-redirect, you have to use the
<strong>R</strong>-flag (see below).</p>
</div>
<p>Additionally you can set special <a name="rewriteflags" id="rewriteflags">flags</a> for <em>Substitution</em> by
appending</p>
<p class="indent">
<strong><code>[</code><em>flags</em><code>]</code></strong>
</p>
<p>
as the third argument to the <code>RewriteRule</code>
directive. <em>Flags</em> is a comma-separated list of the
following flags: </p>
<ul>
<li>'<strong><code>chain|C</code></strong>'
(<strong>c</strong>hained with next rule)<br />
This flag chains the current rule with the next rule
(which itself can be chained with the following rule,
<em>etc.</em>). This has the following effect: if a rule
matches, then processing continues as usual, <em>i.e.</em>,
the flag has no effect. If the rule does
<strong>not</strong> match, then all following chained
rules are skipped. For instance, use it to remove the
"<code>.www</code>" part inside a per-directory rule set
when you let an external redirect happen (where the
"<code>.www</code>" part should not to occur!).</li>
<li>
'<strong><code>cookie|CO=</code></strong><em>NAME</em>:<em>VAL</em>:<em>domain</em>[:<em>lifetime</em>[:<em>path</em>]]'
(set <strong>co</strong>okie)<br />
This sets a cookie on the client's browser. The cookie's name
is specified by <em>NAME</em> and the value is
<em>VAL</em>. The <em>domain</em> field is the domain of the
cookie, such as '.apache.org',the optional <em>lifetime</em>
is the lifetime of the cookie in minutes, and the optional
<em>path</em> is the path of the cookie</li>
<li>
'<strong><code>env|E=</code></strong><em>VAR</em>:<em>VAL</em>'
(set <strong>e</strong>nvironment variable)<br />
This forces an environment variable named <em>VAR</em> to
be set to the value <em>VAL</em>, where <em>VAL</em> can
contain regexp backreferences <code>$N</code>和<code>%N</code> which will be expanded. You can use this
flag more than once to set more than one variable. The
variables can be later dereferenced in many situations, but
usually from within XSSI (via <code><!--#echo
var="VAR"--></code>) or CGI (例如,
<code>$ENV{'VAR'}</code>). Additionally you can dereference
it in a following RewriteCond pattern via
<code>%{ENV:VAR}</code>. Use this to strip but remember
information from URLs.</li>
<li>'<strong><code>forbidden|F</code></strong>' (force URL
to be <strong>f</strong>orbidden)<br />
This forces the current URL to be forbidden,
<em>i.e.</em>, it immediately sends back a HTTP response of
403 (FORBIDDEN). Use this flag in conjunction with
appropriate RewriteConds to conditionally block some
URLs.</li>
<li>'<strong><code>gone|G</code></strong>' (force URL to be
<strong>g</strong>one)<br />
This forces the current URL to be gone, <em>i.e.</em>, it
immediately sends back a HTTP response of 410 (GONE). Use
this flag to mark pages which no longer exist as gone.</li>
<li>
'<strong><code>handler|H</code></strong>=<em>Content-handler</em>'
(force Content <strong>h</strong>andler)<br />
Force the Content-handler of the target file to be
<em>Content-handler</em>. For instance, this can be used to
simulate the <code>mod_alias</code> directive
<code>ScriptAlias</code> which internally forces all files
inside the mapped directory to have a handler of
"<code>cgi-script</code>".</li>
<li>'<strong><code>last|L</code></strong>'
(<strong>l</strong>ast rule)<br />
Stop the rewriting process here and don't apply any more
rewriting rules. This corresponds to the Perl
<code>last</code> command or the <code>break</code> command
from the C language. Use this flag to prevent the currently
rewritten URL from being rewritten further by following
rules. For example, use it to rewrite the root-path URL
('<code>/</code>') to a real one, 例如,'<code>/e/www/</code>'.</li>
<li>'<strong><code>next|N</code></strong>'
(<strong>n</strong>ext round)<br />
Re-run the rewriting process (starting again with the
first rewriting rule). Here the URL to match is again not
the original URL but the URL from the last rewriting rule.
This corresponds to the Perl <code>next</code> command or
the <code>continue</code> command from the C language. Use
this flag to restart the rewriting process, <em>i.e.</em>,
to immediately go to the top of the loop.<br />
<strong>But be careful not to create an infinite
loop!</strong></li>
<li>'<strong><code>nocase|NC</code></strong>'
(<strong>n</strong>o <strong>c</strong>ase)<br />
This makes the <em>Pattern</em> case-insensitive,
<em>i.e.</em>, there is no difference between 'A-Z' and
'a-z' when <em>Pattern</em> is matched against the current
URL.</li>
<li>
'<strong><code>noescape|NE</code></strong>'
(<strong>n</strong>o URI <strong>e</strong>scaping of
output)<br />
This flag keeps mod_rewrite from applying the usual URI
escaping rules to the result of a rewrite. Ordinarily,
special characters (such as '%', '$', ';', and so on)
will be escaped into their hexcode equivalents ('%25',
'%24', and '%3B', respectively); this flag prevents this
from being done. This allows percent symbols to appear in
the output, as in
<div class="example"><p><code>
RewriteRule /foo/(.*) /bar?arg=P1\%3d$1 [R,NE]
</code></p></div>
which would turn '<code>/foo/zed</code>' into a safe
request for '<code>/bar?arg=P1=zed</code>'.
</li>
<li>
'<strong><code>nosubreq|NS</code></strong>' (used only if
<strong>n</strong>o internal
<strong>s</strong>ub-request)<br />
This flag forces the rewriting engine to skip a
rewriting rule if the current request is an internal
sub-request. For instance, sub-requests occur internally
in Apache when <code>mod_include</code> tries to find out
information about possible directory default files
(<code>index.xxx</code>). On sub-requests it is not
always useful and even sometimes causes a failure to if
the complete set of rules are applied. Use this flag to
exclude some rules.<br />
<p>Use the following rule for your decision: whenever you
prefix some URLs with CGI-scripts to force them to be
processed by the CGI-script, the chance is high that you
will run into problems (or even overhead) on
sub-requests. In these cases, use this flag.</p>
</li>
<li>
'<strong><code>proxy|P</code></strong>' (force
<strong>p</strong>roxy)<br />
This flag forces the substitution part to be internally
forced as a proxy request and immediately (<em>i.e.</em>,
rewriting rule processing stops here) put through the <a href="mod_proxy.html">proxy module</a>. You have to make
sure that the substitution string is a valid URI
(例如,typically starting with
<code>http://</code><em>hostname</em>) which can be
handled by the Apache proxy module. If not you get an
error from the proxy module. Use this flag to achieve a
more powerful implementation of the <a href="mod_proxy.html#proxypass">ProxyPass</a> directive,
to map some remote stuff into the namespace of the local
server.
<p>注意:<code class="module"><a href="../mod/mod_proxy.html">mod_proxy</a></code> must be enabled in order
to use this flag.</p>
</li>
<li>
'<strong><code>passthrough|PT</code></strong>'
(<strong>p</strong>ass <strong>t</strong>hrough to next
handler)<br />
This flag forces the rewriting engine to set the
<code>uri</code> field of the internal
<code>request_rec</code> structure to the value of the
<code>filename</code> field. This flag is just a hack to
be able to post-process the output of
<code>RewriteRule</code> directives by
<code>Alias</code>, <code>ScriptAlias</code>, <code>Redirect</code>, <em>etc.</em> directives from
other URI-to-filename translators. A trivial example to
show the semantics: If you want to rewrite
<code>/abc</code> to <code>/def</code> via the rewriting
engine of <code>mod_rewrite</code> and then
<code>/def</code> to <code>/ghi</code> with
<code>mod_alias</code>:
<div class="example"><p><code>
RewriteRule ^/abc(.*) /def$1 [PT]<br />
Alias /def /ghi
</code></p></div>
If you omit the <code>PT</code> flag then
<code>mod_rewrite</code> will do its job fine,
<em>i.e.</em>, it rewrites <code>uri=/abc/...</code> to
<code>filename=/def/...</code> as a full API-compliant
URI-to-filename translator should do. Then
<code>mod_alias</code> comes and tries to do a
URI-to-filename transition which will not work.
<p>Note: <strong>You have to use this flag if you want to
intermix directives of different modules which contain
URL-to-filename translators</strong>. The typical example
is the use of <code>mod_alias</code>和<code>mod_rewrite</code>..</p>
</li>
<li>'<strong><code>qsappend|QSA</code></strong>'
(<strong>q</strong>uery <strong>s</strong>tring
<strong>a</strong>ppend)<br />
This flag forces the rewriting engine to append a query
string part in the substitution string to the existing one
instead of replacing it. Use this when you want to add more
data to the query string via a rewrite rule.</li>
<li>'<strong><code>redirect|R</code>
[=<em>code</em>]</strong>' (force <a id="redirect" name="redirect"><strong>r</strong>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -