📄 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.0</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.0</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 requested
URLs 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="#InternalAPI">API Phases</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#InternalRuleset">Ruleset Processing</a></li>
<li><img alt="" src="../images/down.gif" /> <a href="#InternalBackRefs">Regex Back-Reference Availability</a></li>
<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="#Solutions">Practical Solutions</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="InternalAPI" id="InternalAPI">API Phases</a></h2>
<p>Apache processes a HTTP request in several phases.
A hook for each of these
phases is provided by the Apache API. <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> uses two of
these hooks: the URL-to-filename translation hook
(used after the HTTP request has been read, but before any
authorization starts) and the Fixup hook (triggered
after the authorization phases, and after the per-directory
config files (<code>.htaccess</code>) have been read, but
before the content handler is activated).</p>
<p>Once a request comes in, and Apache has determined the
appropriate server (or virtual server), the rewrite engine
starts the URL-to-filename translation,
processing the mod_rewrite directives from the
per-server configuration. A few
steps later, when the final data directories are found, the
per-directory configuration directives of mod_rewrite are
triggered in the Fixup phase. </p>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="InternalRuleset" id="InternalRuleset">Ruleset Processing</a></h2>
<p>When mod_rewrite is triggered during these two API phases, it
reads the relevant rulesets from its configuration
structure (which was either created on startup, for
per-server context, or during the directory traversal
for per-directory context). The URL rewriting
engine is started with the appropriate ruleset (one or more
rules together with their conditions), and its operation
is exactly the same for both
configuration contexts. Only the final result processing is
different. </p>
<p>The order of rules in the ruleset is important because the
rewrite engine processes them in a particular (not always
obvious) order, as follows: The rewrite engine loops
through the rulesets (each ruleset being made up of <code class="directive"><a href="#rewriterule">RewriteRule</a></code> directives, with or without
<code class="directive"><a href="#rewritecond">RewriteCond</a></code>s), rule by rule.
When a particular rule is matched, <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
also checks the corresponding conditions (<code>RewriteCond</code>
directives). For historical reasons the conditions are given
first, making the control flow a little bit long-winded. See
Figure 1 for more details.</p>
<p class="figure">
<img src="../images/mod_rewrite_fig1.gif" width="428" height="385" alt="[Needs graphics capability to display]" /><br />
<dfn>Figure 1:</dfn>The control flow of the rewrite engine through a
rewrite ruleset
</p>
<p>As above, first the URL is matched against the
<em>Pattern</em> of a rule. If it does not match, <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> immediately stops processing that rule,
and goes on to the next rule. If the <em>Pattern</em> matches,
<code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> checks for rule conditions.
If none are present, the URL will be replaced with a new string,
constructed from the <em>Substitution</em> string, and <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> goes on to the next rule.</p>
<p>If <code class="directive">RewriteCond</code>s exist, an
inner loop is started, processing them in the order that they are
listed. Conditions are not matched against the current URL directly.
A <em>TestString</em> is constructed by expanding variables,
back-references, map lookups, etc., against which the
<em>CondPattern</em> is matched. If the pattern fails to match one
of the conditions, the complete set of rule and associated conditions
fails. If the pattern matches a given condition, then matching continues
to the next condition, until no more conditions are
available. If all conditions match, processing is continued
with the substitution of the <em>Substitution</em> string for the URL.</p>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="InternalBackRefs" id="InternalBackRefs">Regex Back-Reference Availability</a></h2>
<p>Using parentheses in <em>Pattern</em> or in one of the
<em>CondPattern</em>s causes back-references to be internally
created.
These can later be referenced using the strings <code>$N</code> and
<code>%N</code> (see below), for creating
the <em>Substitution</em> and <em>TestString</em> strings.
Figure 2 attempts to show how the back-references are
transferred through the process for later expansion.</p>
<p class="figure">
<img src="../images/mod_rewrite_fig2.gif" width="381" height="179" alt="[Needs graphics capability to display]" /><br />
<dfn>Figure 2:</dfn> The back-reference flow through a rule.
</p>
</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.html
SCRIPT_FILENAME=/u/rse/.www/index.html
SCRIPT_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="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> directly
related 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/def
RewriteBase /xyz
# now the rewriting rules
RewriteRule ^oldstuff\.html$ newstuff.html
</pre></div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -