📄 rewrite_flags.html.en
字号:
<div class="example"><p><code>RewriteRule (.*)A(.*) $1B$2 [N]</code></p></div><p>You can think of this as a <code>while</code> loop: While thispattern still matches, perform this substitution.</p><h3><a name="flag_nc" id="flag_nc">NC|nocase</a></h3><p>Use of the [NC] flag causes the <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> to be matched in acase-insensitive manner. That is, it doesn't care whether letters appearas upper-case or lower-case in the matched URI.</p><p>In the example below, any request for an image file will be proxiedto your dedicated image server. The match is case-insensitive, so that<code>.jpg</code> and <code>.JPG</code> files are both acceptable, forexample.</p><div class="example"><p><code>RewriteRule (.*\.(jpg|gif|png))$ http://images.example.com$1 [P,NC]</code></p></div><h3><a name="flag_ne" id="flag_ne">NE|noescape</a></h3><p>By default, special characters, such as <code>&</code> and<code>?</code>, for example, will be converted to their hexcodeequivalent. Using the [NE] flag prevents that from happening.</p><div class="example"><p><code>RewriteRule ^/anchor/(.+) /bigpage.html#$1 [NE,R]</code></p></div><p>The above example will redirect <code>/anchor/xyz</code> to<code>/bigpage.html#xyz</code>. Omitting the [NE] will result in the #being converted to its hexcode equivalent, <code>%23</code>, which willthen result in a 404 Not Found error condition.</p><h3><a name="flag_ns" id="flag_ns">NS|nosubreq</a></h3><p>Use of the [NS] flag prevents the rule from being used onsubrequests. For example, a page which is included using an SSI (ServerSide Include) is a subrequest, and you may want to avoid rewriteshappening on those subrequests.</p><p>Images, javascript files, or css files, loaded as part of an HTML page,are not subrequests - the browser requests them as separate HTTPrequests.</p><h3><a name="flag_p" id="flag_p">P|proxy</a></h3><p>Use of the [P] flag causes the request to be handled by<code class="module"><a href="../mod/mod_proxy.html">mod_proxy</a></code>, and handled via a proxy request. Forexample, if you wanted all image requests to be handled by a back-endimage server, you might do something like the following:</p><div class="example"><p><code>RewriteRule (.*)\.(jpg|gif|png) http://images.example.com$1.$2 [P]</code></p></div><p>Use of the [P] flag implies [L] - that is, the request is immediatlypushed through the proxy, and any following rules will not beconsidered.</p><h3><a name="flag_pt" id="flag_pt">PT|passthrough</a></h3><p>The target (or substitution string) in a RewriteRule is assumed to be afile path, by default. The use of the [PT] flag causes it to be treatedas a URI instead. That is to say, theuse of the [PT] flag causes the result of the <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> to be passed back throughURL mapping, so that location-based mappings, such as <code class="directive"><a href="../mod/mod_alias.html#alias">Alias</a></code>, for example, might have a chance to takeeffect.</p><p>If, for example, you have an <code class="directive"><a href="../mod/mod_alias.html#alias">Alias</a></code>for /icons, and have a <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> pointing there, you shoulduse the [PT] flag to ensure that the <code class="directive"><a href="../mod/mod_alias.html#alias">Alias</a></code> is evaluated.</p><div class="example"><p><code>Alias /icons /usr/local/apache/icons<br />RewriteRule /pics/(.+)\.jpg /icons/$1.gif [PT]</code></p></div><p>Omission of the [PT] flag in this case will cause the Alias to beignored, resulting in a 'File not found' error being returned.</p><h3><a name="flag_qsa" id="flag_qsa">QSA|qsappend</a></h3><p>When the replacement URI contains a query string, the default behaviorof <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> is to discardthe existing query string, and replace it with the newly generated one.Using the [QSA] flag causes the query strings to be combined.</p><p>Consider the following rule:</p><div class="example"><p><code>RewriteRule /pages/(.+) /page.php?page=$1 [QSA]</code></p></div><p>With the [QSA] flag, a request for <code>/pages/123?one=two</code> will bemapped to <code>/page.php?page=123&one=two</code>. Without the [QSA]flag, that same request will be mapped to<code>/page.php?page=123</code> - that is, the existing query stringwill be discarded.</p><h3><a name="flag_r" id="flag_r">R|redirect</a></h3><p>Use of the [R] flag causes a HTTP redirect to be issued to the browser.If a fully-qualified URL is specified (that is, including<code>http://servername/</code>) then a redirect will be issued to thatlocation. Otherwise, the current servername will be used to generate theURL sent with the redirect.</p><p>A status code may be specified, in the range 300-399, with a 302 statuscode being used by default if none is specified.</p><p>You will almost always want to use [R] in conjunction with [L] (that is,use [R,L]) because on its own, the [R] flag prepends<code>http://thishost[:thisport]</code> to the URI, but then passes thison to the next rule in the ruleset, which can often result in 'InvalidURI in request' warnings.</p><h3><a name="flag_s" id="flag_s">S|skip</a></h3><p>The [S] flag is used to skip rules that you don't want to run. Thiscan be thought of as a <code>goto</code> statement in your rewriteruleset. In the following example, we only want to run the <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> if the requested URIdoesn't correspond with an actual file.</p><div class="example"><p><code># Is the request for a non-existent file?RewriteCond %{REQUEST_FILENAME} !-f<br />RewriteCond %{REQUEST_FILENAME} !-d<br /># If so, skip these two RewriteRulesRewriteRule .? - [S=2]<br /><br />RewriteRule (.*\.gif) images.php?$1<br />RewriteRule (.*\.html) docs.php?$1</code></p></div><p>This technique is useful because a <code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code> only applies to the<code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> immediatelyfollowing it. Thus, if you want to make a <code>RewriteCond</code> applyto several <code>RewriteRule</code>s, one possible technique is tonegate those conditions and use a [Skip] flag.</p><h3><a name="flag_t" id="flag_t">T|type</a></h3><p>Sets the MIME type with which the resulting response will besent. This has the same effect as the <code class="directive"><a href="../mod/mod_mime.html#addtype">AddType</a></code> directive.</p><p>For example, you might use the following technique to serve Perlsource code as plain text, if requested in a particular way:</p><div class="example"><p><code># Serve .pl files as plain textRewriteRule \.pl$ - [T=text/plain]</code></p></div><p>Or, perhaps, if you have a camera that produces jpeg images withoutfile extensions, you could force those images to be served with thecorrect MIME type by virtue of their file names:</p><div class="example"><p><code># Files with 'IMG' in the name are jpg images.<br />RewriteRule IMG - [T=image/jpg]</code></p></div><p>Please note that this is a trivial example, and could be better doneusing <code class="directive"><a href="../mod/core.html#filesmatch"><FilesMatch></a></code>instead. Always consider the alternatesolutions to a problem before resorting to rewrite, which willinvariably be a less efficient solution than the alternatives.</p></div></div><div class="bottomlang"><p><span>Available Languages: </span><a href="../en/rewrite/rewrite_flags.html" title="English"> en </a></p></div><div id="footer"><p class="apache">Copyright 2008 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p><p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -