📄 rewrite_intro.html.en
字号:
<ol><li><var>Pattern</var>: which incoming URLs should be affected by the rule;</li><li><var>Substitution</var>: where should the matching requests be sent;</li><li><var>[flags]</var>: options affecting the rewritten request.</li></ol><p>The <var>Pattern</var> is always a <a href="#regex">regularexpression</a> matched against the URL-Path of the incoming request(the part after the hostname but before any question mark indicatingthe beginning of a query string).</p><p>The <var>Substitution</var> can itself be one of three things:</p><dl><dt>A full filesystem path to a resource</dt><dd><div class="example"><p><code>RewriteRule ^/games.* /usr/local/games/web</code></p></div><p>This maps a request to an arbitrary location on your filesystem, muchlike the <code class="directive"><a href="../mod/mod_alias.html#alias">Alias</a></code> directive.</p></dd><dt>A web-path to a resource</dt><dd><div class="example"><p><code>RewriteRule ^/foo$ /bar</code></p></div><p>If <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code> is setto <code>/usr/local/apache2/htdocs</code>, then this directive wouldmap requests for <code>http://example.com/foo</code> to thepath <code>/usr/local/apache2/htdocs/bar</code>.</p></dd><dt>An absolute URL</dt><dd><div class="example"><p><code>RewriteRule ^/product/view$ http://site2.example.com/seeproduct.html [R]</code></p></div><p>This tells the client to make a new request for the specified URL.</p></dd></dl><p>The <var>Substitution</var> can alsocontain <em>back-references</em> to parts of the incoming URL-pathmatched by the <var>Pattern</var>. Consider the following:</p><div class="example"><p><code>RewriteRule ^/product/(.*)/view$ /var/web/productdb/$1</code></p></div><p>The variable <code>$1</code> will be replaced with whatever textwas matched by the expression inside the parenthesis inthe <var>Pattern</var>. For example, a requestfor <code>http://example.com/product/r14df/view</code> will be mappedto the path <code>/var/web/productdb/r14df</code>.</p><p>If there is more than one expression in parenthesis, they areavailable in order in thevariables <code>$1</code>, <code>$2</code>, <code>$3</code>, and soon.</p></div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="section"><h2><a name="flags" id="flags">Rewrite Flags</a></h2><p>The behavior of a <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> can be modified by theapplication of one or more flags to the end of the rule. For example, thematching behavior of a rule can be made case-insensitive by theapplication of the <code>[NC]</code> flag:</p><div class="example"><p><code>RewriteRule ^puppy.html smalldog.html [NC]</code></p></div><p>For more details on the available flags, their meanings, andexamples, see the <a href="rewrite_flags.html">Rewrite Flags</a> document.</p></div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="section"><h2><a name="rewritecond" id="rewritecond">Rewrite conditions</a></h2><p>One or more <code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code>directives can be used to restrict the types of requests that will besubject to thefollowing <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code>. Thefirst argument is a variable describing a characteristic of therequest, the second argument is a <a href="#regex">regularexpression</a> that must match the variable, and a third optionalargument is a list of flags that modify how the match is evaluated.</p><p>For example, to send all requests from a particular IP range to adifferent server, you could use:</p><div class="example"><p><code>RewriteCond %{REMOTE_ADDR} ^10\.2\.<br />RewriteRule (.*) http://intranet.example.com$1</code></p></div><p>When more thanone <code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code> isspecified, they must all match forthe <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> to beapplied. For example, to deny requests that contain the word "hack" intheir query string, except if they also contain a cookie containingthe word "go", you could use:</p><div class="example"><p><code>RewriteCond %{QUERY_STRING} hack<br />RewriteCond %{HTTP_COOKIE} !go<br />RewriteRule .* - [F]</code></p></div><p>Notice that the exclamation mark specifies a negative match, so the rule is only applied if the cookie does not contain "go".</p><p>Matches in the regular expressions contained inthe <code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code>s can beused as part of the <var>Substitution</var> inthe <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> using thevariables <code>%1</code>, <code>%2</code>, etc. For example, thiswill direct the request to a different directory depending on thehostname used to access the site:</p><div class="example"><p><code>RewriteCond %{HTTP_HOST} (.*)<br />RewriteRule ^/(.*) /sites/%1/$1</code></p></div><p>If the request was for <code>http://example.com/foo/bar</code>,then <code>%1</code> would contain <code>example.com</code>and <code>$1</code> would contain <code>foo/bar</code>.</p></div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="section"><h2><a name="rewritemap" id="rewritemap">Rewrite maps</a></h2><p>See <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>.</p></div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="section"><h2><a name="htaccess" id="htaccess">.htaccess files</a></h2><p>Rewriting is typically configured in the main server configurationsetting (outside any <code class="directive"><a href="../mod/core.html#directory"><Directory></a></code> section) orinside <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code>containers. This is the easiest way to do rewriting and isrecommended. It is possible, however, to do rewritinginside <code class="directive"><a href="../mod/core.html#directory"><Directory></a></code>sections or <a href="../howto/htaccess.html"><code>.htaccess</code>files</a> at the expense of some additional complexity. This techniqueis called per-directory rewrites.</p><p>The main difference with per-server rewrites is that the pathprefix of the directory containing the <code>.htaccess</code> file isstripped before matching inthe <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code>. In addition, the <code class="directive"><a href="../mod/mod_rewrite.html#rewritebase">RewriteBase</a></code> should be used to assure the request is properly mapped.</p></div></div><div class="bottomlang"><p><span>Available Languages: </span><a href="../en/rewrite/rewrite_intro.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 + -