⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mod_rewrite.html

📁 这个是我在web培训时老师提供的手册
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="keywords" content="Apache, 中文, 手册, 中文版, 中文手册, 中文版手册, 参考手册, 中文参考手册, 金步国" />
<meta name="description" content="Apache 2.2 中文版参考手册" />
<meta name="author" content="金步国" />
<link href="../style/css/manual-zip.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
<link href="../style/css/manual-zip-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" />
<title>mod_rewrite - Apache 2.2 中文版参考手册</title>
</head>
<body><div id="page-header">
<p class="menu"><a href="../mod/index.html">模块索引</a> | <a href="../mod/directives.html">指令索引</a> | <a href="../faq/index.html">常见问题</a> | <a href="../glossary.html">词汇表</a> | <a href="../sitemap.html">站点导航</a></p><p class="apache">Apache HTTP Server 版本2.2</p><img alt="" src="../images/feather.gif" /></div>
<div class="up"><a href="./index.html"><img title="&lt;-" alt="&lt;-" src="../images/left.gif" /></a></div>
<div id="path"><a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">文档</a> &gt; <a href="../index.html">版本2.2</a> &gt; <a href="./index.html">模块</a></div>
 
<div id="translation-info">   <a href="../translator_announcement.html#thanks">致谢</a> | <a href="../translator_announcement.html#announcement">译者声明</a> | 本篇译者:&lt;<a href="../translator_announcement.html#join">虚位以待</a>&gt; | 本篇译稿完成时间:?年?月?日 | <a href="../translator_announcement.html#last_new">获取最新版本</a></div>
<div id="page-content"><div id="preamble"><h1>Apache模块 mod_rewrite</h1>

<table border="1" cellpadding="0" cellspacing="0" bordercolor="#AAAAAA" class="module">
<tr><th><a href="module-dict.html#Description">说明</a></th><td>一个基于一定规则的实时重写URL请求的引擎</td></tr>
<tr><th><a href="module-dict.html#Status">状态</a></th><td>扩展(E)</td></tr>
<tr><th><a href="module-dict.html#ModuleIdentifier">模块名</a></th><td>rewrite_module</td></tr>
<tr><th><a href="module-dict.html#SourceFile">源文件</a></th><td>mod_rewrite.c</td></tr>
<tr><th><a href="module-dict.html#Compatibility">兼容性</a></th><td>仅在 Apache 1.3 及以后的版本中可用</td></tr>
</table>
<h3>概述</h3>

      <p>此模块提供了一个基于正则表达式分析器的重写引擎来实时重写URL请求。它支持每个完整规则可以拥有不限数量的子规则以及附加条件规则的灵活而且强大的URL操作机制。此URL操作可以依赖于各种测试,比如服务器变量、环境变量、HTTP头、时间标记,甚至各种格式的用于匹配URL组成部分的查找数据库。</p>
      <p>此模块可以操作URL的所有部分(包括路径信息部分),在服务器级的(<code>httpd.conf</code>)和目录级的(<code>.htaccess</code>)配置都有效,还可以生成最终请求字符串。此重写操作的结果可以是内部子处理,也可以是外部请求的转向,甚至还可以是内部代理处理。</p>
	<p>但是,所有这些功能和灵活性带来一个问题,那就是复杂性,因此,不要指望一天之内就能看懂整个模块。</p>
      <p>更多的讨论、细节、示例,请查看详细的<a href="../rewrite/index.html">URL重写文档</a>。</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">特殊字符的引用</a></h2>

      <p>在Apache 1.3.20中,<em>TestString</em>和<em>Substitution</em>中的特殊字符可以用前导斜杠(\)来实现转义(即忽略其特殊含义而视之为普通字符)。 比如,<em>Substitution</em>可以用"<code>\$</code>"来包含一个美元符号,以避免mod_rewrite把它视为反向引用。</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">环境变量</a></h2>

      <p>此模块会跟踪两个额外的(非标准)CGI/SSI环境变量,<code>SCRIPT_URL</code>和<code>SCRIPT_URI</code>。他们包含了当前资源的<em>逻辑</em>网络视图,而标准CGI/SSI变量<code>SCRIPT_NAME</code>和<code>SCRIPT_FILENAME</code>包含的是<em>物理</em>系统视图。</p>

      <p>注意:这些变量保持的是<em>其最初被请求时的</em>URI/URL,即在任何重写操作<em>之前</em>的URI/URL。其重要性在于他们是重写操作重写URL到物理路径名的原始依据。</p>

<div class="example"><h3>示例</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">实用方案</a></h2>

    <p>我们提供了<a href="../rewrite/rewrite_guide.html">URL重写指南</a>和<a href="../rewrite/rewrite_guide_advanced.html">高级URL重写指南</a>文档,列举了许多基于URL的问题的实用方案,其中你可以找到真实有用的规则集。</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">指令</a></h2>
<table border="1" cellpadding="0" cellspacing="0" bordercolor="#AAAAAA" class="directive">
<tr><th><a href="directive-dict.html#Description">说明</a></th><td>设置目录级重写的基准URL</td></tr>
<tr><th><a href="directive-dict.html#Syntax">语法</a></th><td><code>RewriteBase <em>URL-path</em></code></td></tr>
<tr><th><a href="directive-dict.html#Default">默认值</a></th><td><code>参见使用方法</code></td></tr>
<tr><th><a href="directive-dict.html#Context">作用域</a></th><td>directory, .htaccess</td></tr>
<tr><th><a href="directive-dict.html#Override">覆盖项</a></th><td>FileInfo</td></tr>
<tr><th><a href="directive-dict.html#Status">状态</a></th><td>扩展(E)</td></tr>
<tr><th><a href="directive-dict.html#Module">模块</a></th><td>mod_rewrite</td></tr>
</table>
      <p><code class="directive">RewriteBase</code>指令显式地设置了目录级重写的基准URL。在下文中,你可以看见<code class="directive"><a href="#rewriterule">RewriteRule</a></code>可以用于目录级的配置文件中(<code>.htaccess</code>)并在局部范围内起作用,即规则实际处理的只是剥离了本地路径前缀的一部分。处理结束后,这个路径会被自动地附着回去。默认值是"<code class="directive">RewriteBase</code> <em>physical-directory-path</em>"。</p>

      <p>在对一个新的URL进行替换时,此模块必须把这个URL重新注入到服务器处理中。为此,它必须知道其对应的URL前缀或者说URL基准。通常,此前缀就是对应的文件路径。<strong>但是,大多数网站URL不是直接对应于其物理文件路径的,因而一般不能做这样的假定! </strong>所以在这种情况下,就必须用<code>RewriteBase</code>指令来指定正确的URL前缀。</p>

<div class="note">如果你的网站服务器URL<strong>不是</strong>与物理文件路径直接对应的,而又需要使用<code class="directive">RewriteBase</code>指令,则必须在每个对应的<code>.htaccess</code>文件中指定<code class="directive"><a href="#rewriterule">RewriteRule</a></code> 。</div>

        <p>例如,目录级配置文件内容如下:</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 例如,
#

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>

        <p>上述例子中,对<code>/xyz/oldstuff.html</code>的请求被正确地重写为物理的文件<code>/abc/def/newstuff.html</code> 。</p>

<div class="note"><h3>For Apache Hackers</h3>
<p>以下列出了内部处理的详细步骤:</p>
<pre>
Request:
  /xyz/oldstuff.html

Internal Processing:
  /xyz/oldstuff.html     -&gt; /abc/def/oldstuff.html  (per-server Alias)
  /abc/def/oldstuff.html -&gt; /abc/def/newstuff.html  (per-dir    RewriteRule)
  /abc/def/newstuff.html -&gt; /xyz/newstuff.html      (per-dir    RewriteBase)
  /xyz/newstuff.html     -&gt; /abc/def/newstuff.html  (per-server Alias)

Result:
  /abc/def/newstuff.html
</pre>
              <p>虽然这个过程看来很繁复,但是由于目录级重写的到来时机已经太晚了,它不得不把这个(重写)请求重新注入到Apache核心中,所以Apache内部确实是这样处理的。但是:它的开销并不象看起来的那样大,因为重新注入完全在Apache服务器内部进行,而且这样的过程在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">指令</a></h2>
<table border="1" cellpadding="0" cellspacing="0" bordercolor="#AAAAAA" class="directive">
<tr><th><a href="directive-dict.html#Description">说明</a></th><td>定义重写发生的条件</td></tr>
<tr><th><a href="directive-dict.html#Syntax">语法</a></th><td><code> RewriteCond <em>TestString</em> <em>CondPattern</em></code></td></tr>
<tr><th><a href="directive-dict.html#Context">作用域</a></th><td>server config, virtual host, directory, .htaccess</td></tr>
<tr><th><a href="directive-dict.html#Override">覆盖项</a></th><td>FileInfo</td></tr>
<tr><th><a href="directive-dict.html#Status">状态</a></th><td>扩展(E)</td></tr>
<tr><th><a href="directive-dict.html#Module">模块</a></th><td>mod_rewrite</td></tr>
</table>
      <p><code class="directive">RewriteCond</code>指令定义了一个规则的条件,即在一个<code class="directive"><a href="#rewriterule">RewriteRule</a></code>指令之前有一个或多个<code class="directive">RewriteCond</code>指令。条件之后的重写规则仅在当前URI与pattern匹配<strong>并且</strong>符合这些条件的时候才会起作用。</p>

      <p><em>TestString</em>是一个纯文本的字符串,但是还可以包含下列可扩展的成分:</p>

      <ul>
        <li>
          <strong>RewriteRule反向引用</strong> ,引用方法是:

          <p class="indent">
            <strong><code>$N</code></strong>
          </p>(0 &lt;= N &lt;= 9)引用当前(带有若干<code>RewriteRule</code>指令的)<code>RewriteCond</code>中的与pattern匹配的分组成分(圆括号!)。</li>

        <li>
          <strong>RewriteCond反向引用</strong> ,引用方法是:
          <p class="indent">
            <strong><code>%N</code></strong>
          </p>(1 &lt;= N &lt;= 9)引用当前若干<code>RewriteCond</code>条件中最后符合的条件中的分组成分(圆括号!)。</li>

        <li>
          <strong>RewriteMap扩展</strong> ,引用方法是:
          <p class="indent">
            <strong><code>${mapname:key|default}</code></strong>
          </p>细节请参见<a href="#mapfunc">RewriteMap 指令</a>。
        </li>

        <li>
          <strong>服务器变量</strong> ,引用方法是:
          <p class="indent">
            <strong><code>%{</code> <em>NAME_OF_VARIABLE</em> <code>}</code></strong>
          </p><em>NAME_OF_VARIABLE</em>可以是下表列出的字符串之一:
          <table border="1" cellpadding="0" cellspacing="0" bordercolor="#AAAAAA">
<tr><th>HTTP headers:</th><th>connection &amp; request:</th><th /></tr>
<tr><td>
		 HTTP_USER_AGENT<br />
                 HTTP_REFERER<br />
                 HTTP_COOKIE<br />
                 HTTP_FORWARDED<br />
                 HTTP_HOST<br />
                 HTTP_PROXY_CONNECTION<br />
                 HTTP_ACCEPT<br />
              </td><td>
                 REMOTE_ADDR<br />
                 REMOTE_HOST<br />
                 REMOTE_PORT<br />
                 REMOTE_USER<br />
                 REMOTE_IDENT<br />
                 REQUEST_METHOD<br />
                 SCRIPT_FILENAME<br />
                 PATH_INFO<br />
                 QUERY_STRING<br />
                 AUTH_TYPE<br />
              </td><td /></tr>
<tr><th>server internals:</th><th>date and time:</th><th>specials:</th></tr>
<tr><td>
	         DOCUMENT_ROOT<br />
                 SERVER_ADMIN<br />
                 SERVER_NAME<br />
                 SERVER_ADDR<br />
                 SERVER_PORT<br />
                 SERVER_PROTOCOL<br />
                 SERVER_SOFTWARE<br />
              </td><td>
                 TIME_YEAR<br />
                 TIME_MON<br />
                 TIME_DAY<br />
                 TIME_HOUR<br />
                 TIME_MIN<br />
                 TIME_SEC<br />
                 TIME_WDAY<br />
                 TIME<br />
              </td><td>
                 API_VERSION<br />
                 THE_REQUEST<br />
                 REQUEST_URI<br />
                 REQUEST_FILENAME<br />
                 IS_SUBREQ<br />
                 HTTPS<br />
              </td></tr>
</table>

<div class="note">
                <p>These variables all
                correspond to the similarly named HTTP
                MIME-headers, C variables of the Apache server or
                <code>struct tm</code> fields of the Unix system.
                Most are documented elsewhere in the Manual or in
                the CGI specification. Those that are special to
                mod_rewrite include:</p>

                <dl>
                  <dt><code>IS_SUBREQ</code></dt>

                  <dd>Will contain the text "true" if the request
                  currently being processed is a sub-request,
                  "false" otherwise. Sub-requests may be generated
                  by modules that need to resolve additional files
                  or URIs in order to complete their tasks.</dd>

                  <dt><code>API_VERSION</code></dt>

                  <dd>This is the version of the Apache module API
                  (the internal interface between server and
                  module) in the current httpd build, as defined in
                  include/ap_mmn.h. The module API version
                  corresponds to the version of Apache in use (in
                  the release version of Apache 1.3.14, for
                  instance, it is 19990320:10), but is mainly of
                  interest to module authors.</dd>

                  <dt><code>THE_REQUEST</code></dt>

                  <dd>The full HTTP request line sent by the
                  browser to the server (e.g., "<code>GET
                  /index.html HTTP/1.1</code>"). This does not
                  include any additional headers sent by the
                  browser.</dd>

                  <dt><code>REQUEST_URI</code></dt>

                  <dd>The resource requested in the HTTP request
                  line. (In the example above, this would be
                  "/index.html".)</dd>

                  <dt><code>REQUEST_FILENAME</code></dt>

                  <dd>The full local filesystem path to the file or

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -