📄 mod_rewrite.html.en
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!-- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX This file is generated from xml source: DO NOT EDIT XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX --><title>mod_rewrite - Apache HTTP Server</title><link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" /><link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" /><link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link href="../images/favicon.ico" rel="shortcut icon" /></head><body><div id="page-header"><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><p class="apache">Apache HTTP Server Version 2.2</p><img alt="" src="../images/feather.gif" /></div><div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div><div id="path"><a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.2</a> > <a href="./">Modules</a></div><div id="page-content"><div id="preamble"><h1>Apache Module mod_rewrite</h1><div class="toplang"><p><span>Available Languages: </span><a href="../en/mod/mod_rewrite.html" title="English"> en </a></p></div><table class="module"><tr><th><a href="module-dict.html#Description">Description:</a></th><td>Provides a rule-based rewriting engine to rewrite requestedURLs on the fly</td></tr><tr><th><a href="module-dict.html#Status">Status:</a></th><td>Extension</td></tr><tr><th><a href="module-dict.html#ModuleIdentifier">Module營dentifier:</a></th><td>rewrite_module</td></tr><tr><th><a href="module-dict.html#SourceFile">Source燜ile:</a></th><td>mod_rewrite.c</td></tr><tr><th><a href="module-dict.html#Compatibility">Compatibility:</a></th><td>Available in Apache 1.3 and later</td></tr></table><h3>Summary</h3> <p>This module uses a rule-based rewriting engine (based on a regular-expression parser) to rewrite requested URLs on the fly. It supports an unlimited number of rules and an unlimited number of attached rule conditions for each rule, to provide a really flexible and powerful URL manipulation mechanism. The URL manipulations can depend on various tests, of server variables, environment variables, HTTP headers, or time stamps. Even external database lookups in various formats can be used to achieve highly granular URL matching.</p> <p>This module operates on the full URLs (including the path-info part) both in per-server context (<code>httpd.conf</code>) and per-directory context (<code>.htaccess</code>) and can generate query-string parts on result. The rewritten result can lead to internal sub-processing, external request redirection or even to an internal proxy throughput.</p> <p>Further details, discussion, and examples, are provided in the <a href="../rewrite/">detailed mod_rewrite documentation</a>.</p></div><div id="quickview"><h3 class="directives">Directives</h3><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#rewritebase">RewriteBase</a></li><li><img alt="" src="../images/down.gif" /> <a href="#rewritecond">RewriteCond</a></li><li><img alt="" src="../images/down.gif" /> <a href="#rewriteengine">RewriteEngine</a></li><li><img alt="" src="../images/down.gif" /> <a href="#rewritelock">RewriteLock</a></li><li><img alt="" src="../images/down.gif" /> <a href="#rewritelog">RewriteLog</a></li><li><img alt="" src="../images/down.gif" /> <a href="#rewriteloglevel">RewriteLogLevel</a></li><li><img alt="" src="../images/down.gif" /> <a href="#rewritemap">RewriteMap</a></li><li><img alt="" src="../images/down.gif" /> <a href="#rewriteoptions">RewriteOptions</a></li><li><img alt="" src="../images/down.gif" /> <a href="#rewriterule">RewriteRule</a></li></ul><h3>Topics</h3><ul id="topics"><li><img alt="" src="../images/down.gif" /> <a href="#quoting">Quoting Special Characters</a></li><li><img alt="" src="../images/down.gif" /> <a href="#EnvVar">Environment Variables</a></li><li><img alt="" src="../images/down.gif" /> <a href="#vhosts">Rewriting in Virtual Hosts</a></li><li><img alt="" src="../images/down.gif" /> <a href="#Solutions">Practical Solutions</a></li></ul><h3>See also</h3><ul class="seealso"><li><a href="#rewriteflags">Rewrite Flags</a></li></ul></div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="section"><h2><a name="quoting" id="quoting">Quoting Special Characters</a></h2> <p>As of Apache 1.3.20, special characters in <em>TestString</em> and <em>Substitution</em> strings can be escaped (that is, treated as normal characters without their usual special meaning) by prefixing them with a slash ('\') character. In other words, you can include an actual dollar-sign character in a <em>Substitution</em> string by using '<code>\$</code>'; this keeps mod_rewrite from trying to treat it as a backreference.</p></div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="section"><h2><a name="EnvVar" id="EnvVar">Environment Variables</a></h2> <p>This module keeps track of two additional (non-standard) CGI/SSI environment variables named <code>SCRIPT_URL</code> and <code>SCRIPT_URI</code>. These contain the <em>logical</em> Web-view to the current resource, while the standard CGI/SSI variables <code>SCRIPT_NAME</code> and <code>SCRIPT_FILENAME</code> contain the <em>physical</em> System-view. </p> <p>Notice: These variables hold the URI/URL <em>as they were initially requested</em>, that is, <em>before</em> any rewriting. This is important to note because the rewriting process is primarily used to rewrite logical URLs to physical pathnames.</p><div class="example"><h3>Example</h3><pre>SCRIPT_NAME=/sw/lib/w3s/tree/global/u/rse/.www/index.htmlSCRIPT_FILENAME=/u/rse/.www/index.htmlSCRIPT_URL=/u/rse/SCRIPT_URI=http://en1.engelschall.com/u/rse/</pre></div></div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="section"><h2><a name="vhosts" id="vhosts">Rewriting in Virtual Hosts</a></h2> <p>By default, <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> configuration settings from the main server context are not inherited by virtual hosts. To make the main server settings apply to virtual hosts, you must place the following directives in each <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code> section:</p> <div class="example"><p><code> RewriteEngine On<br /> RewriteOptions Inherit </code></p></div></div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="section"><h2><a name="Solutions" id="Solutions">Practical Solutions</a></h2> <p>For numerous examples of common, and not-so-common, uses for mod_rewrite, see the <a href="../rewrite/rewrite_guide.html">Rewrite Guide</a>, and the <a href="../rewrite/rewrite_guide_advanced.html">Advanced Rewrite Guide</a> documents.</p></div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="directive-section"><h2><a name="RewriteBase" id="RewriteBase">RewriteBase</a> <a name="rewritebase" id="rewritebase">Directive</a></h2><table class="directive"><tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Sets the base URL for per-directory rewrites</td></tr><tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>RewriteBase <em>URL-path</em></code></td></tr><tr><th><a href="directive-dict.html#Default">Default:</a></th><td><code>See usage for information.</code></td></tr><tr><th><a href="directive-dict.html#Context">Context:</a></th><td>directory, .htaccess</td></tr><tr><th><a href="directive-dict.html#Override">Override:</a></th><td>FileInfo</td></tr><tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Extension</td></tr><tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_rewrite</td></tr></table> <p>The <code class="directive">RewriteBase</code> directive explicitly sets the base URL for per-directory rewrites. As you will see below, <code class="directive"><a href="#rewriterule">RewriteRule</a></code> can be used in per-directory config files (<code>.htaccess</code>). In such a case, it will act locally, stripping the local directory prefix before processing, and applying rewrite rules only to the remainder. When processing is complete, the prefix is automatically added back to the path. The default setting is; <code class="directive">RewriteBase</code> <em>physical-directory-path</em></p> <p>When a substitution occurs for a new URL, this module has to re-inject the URL into the server processing. To be able to do this it needs to know what the corresponding URL-prefix or URL-base is. By default this prefix is the corresponding filepath itself. <strong>However, for most websites, URLs are NOT directly related to physical filename paths, so this assumption will often be wrong!</strong> Therefore, you can use the <code>RewriteBase</code> directive to specify the correct URL-prefix.</p><div class="note"> If your webserver's URLs are <strong>not</strong> directlyrelated to physical file paths, you will need to use<code class="directive">RewriteBase</code> in every <code>.htaccess</code>file where you want to use <code class="directive"><a href="#rewriterule">RewriteRule</a></code> directives.</div> <p> For example, assume the following per-directory config file:</p><div class="example"><pre>## /abc/def/.htaccess -- per-dir config file for directory /abc/def# Remember: /abc/def is the physical path of /xyz, <em>i.e.</em>, the server# has a 'Alias /xyz /abc/def' directive <em>e.g.</em>#RewriteEngine On# let the server know that we were reached via /xyz and not# via the physical path prefix /abc/defRewriteBase /xyz# now the rewriting rulesRewriteRule ^oldstuff\.html$ newstuff.html</pre></div> <p>In the above example, a request to <code>/xyz/oldstuff.html</code> gets correctly rewritten to the physical file <code>/abc/def/newstuff.html</code>.</p><div class="note"><h3>For Apache Hackers</h3><p>The following list gives detailed information about the internal processing steps:</p><pre>Request: /xyz/oldstuff.htmlInternal Processing: /xyz/oldstuff.html -> /abc/def/oldstuff.html (per-server Alias) /abc/def/oldstuff.html -> /abc/def/newstuff.html (per-dir RewriteRule) /abc/def/newstuff.html -> /xyz/newstuff.html (per-dir RewriteBase) /xyz/newstuff.html -> /abc/def/newstuff.html (per-server Alias)Result: /abc/def/newstuff.html</pre> <p>This seems very complicated, but is in fact correct Apache internal processing. Because the per-directory rewriting comes late in the process, the rewritten request has to be re-injected into the Apache kernel, as if it were a new request. (See <a href="../rewrite/rewrite_tech.html">mod_rewrite technical details</a>.) This is not the serious overhead it may seem to be - this re-injection is completely internal to the Apache server (and the same procedure is used by many other operations within Apache).</p> </div></div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="directive-section"><h2><a name="RewriteCond" id="RewriteCond">RewriteCond</a> <a name="rewritecond" id="rewritecond">Directive</a></h2><table class="directive"><tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Defines a condition under which rewriting will take place</td></tr><tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code> RewriteCond <em>TestString</em> <em>CondPattern</em></code></td></tr><tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host, directory, .htaccess</td></tr><tr><th><a href="directive-dict.html#Override">Override:</a></th><td>FileInfo</td></tr><tr><th><a href="directive-dict.html#Status">Status:</a></th><td>Extension</td></tr><tr><th><a href="directive-dict.html#Module">Module:</a></th><td>mod_rewrite</td></tr></table> <p>The <code class="directive">RewriteCond</code> directive defines a rule condition. One or more <code class="directive">RewriteCond</code> can precede a <code class="directive"><a href="#rewriterule">RewriteRule</a></code> directive. The following rule is then only used if both the current state of the URI matches its pattern, <strong>and</strong> if these conditions are met.</p> <p><em>TestString</em> is a string which can contain the following expanded constructs in addition to plain text:</p> <ul> <li> <strong>RewriteRule backreferences</strong>: These are backreferences of the form <strong><code>$N</code></strong> (0 <= N <= 9), which provide access to the grouped parts (in parentheses) of the pattern, from the <code>RewriteRule</code> which is subject to the current set of <code>RewriteCond</code> conditions.. </li> <li> <strong>RewriteCond backreferences</strong>: These are backreferences of the form <strong><code>%N</code></strong> (1 <= N <= 9), which provide access to the grouped parts (again, in parentheses) of the pattern, from the last matched <code>RewriteCond</code> in the current set of conditions. </li> <li> <strong>RewriteMap expansions</strong>: These are expansions of the form <strong><code>${mapname:key|default}</code></strong>. See <a href="#mapfunc">the documentation for RewriteMap</a> for more details. </li> <li>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -