📄 mod_rewrite+.html
字号:
<p class="indent">
<strong><em>MatchingKey</em>
<em>SubstValue</em></strong>
</p>
<div class="example"><h3>Example</h3><pre>
##
## map.txt -- rewriting map
##
Ralf.S.Engelschall rse # Bastard Operator From Hell
Mr.Joe.Average joe # Mr. Average
</pre></div>
<div class="example"><p><code>
RewriteMap real-to-user txt:/path/to/file/map.txt
</code></p></div>
</li>
<li>
<strong>随机纯文本</strong><br />
MapType: <code>rnd</code>, MapSource: 有效的Unix文件系统文件名
<p>这个与上述的标准纯文本很相似,但它有一个特殊的后处理特性:
查找完毕后,会解析其中包含的含义为``or''和``<code>|</code>''符号。
也就是说,会随机地选择其中之一作为实际的返回值。
虽然这看似毫无意义,但它的设计意图是,
在一个查找值是服务器名称的反向代理环境中,实现负载平衡。如:</p>
<div class="example"><pre>
##
## map.txt -- rewriting map
##
static www1|www2|www3|www4
dynamic www5|www6
</pre></div>
<div class="example"><p><code>
RewriteMap servers rnd:/path/to/file/map.txt
</code></p></div>
</li>
<li>
<strong>散列文件</strong><br /> MapType:
<code>dbm[=<em>type</em>]</code>, MapSource: 有效的Unix文件系统文件名
<p>这里的源是一个二进制格式的DBM文件,包含了与<em>纯文本</em>相同的内容,
但是因为它有优化的特殊表现形式,使它的查找速度明显快得多。
此<em>类型</em>可以是sdbm, gdbm, ndbm或db,由<a href="../install.html#dbm">compile-time
settings</a>所决定。如果省略<em>type</em>,则使用编译时选择的缺省设置。
你可以使用任何DBM工具或者下列Perl脚本来建立这个文件,但必须保证DBM的类型正确。
建立NDBM文件的例子:</p>
<div class="example"><pre>
#!/path/to/bin/perl
##
## txt2dbm -- convert txt map to dbm format
##
use NDBM_File;
use Fcntl;
($txtmap, $dbmmap) = @ARGV;
open(TXT, "<$txtmap") or die "Couldn't open $txtmap!\n";
tie (%DB, 'NDBM_File', $dbmmap,O_RDWR|O_TRUNC|O_CREAT, 0644)
or die "Couldn't create $dbmmap!\n";
while (<TXT>) {
next if (/^\s*#/ or /^\s*$/);
$DB{$1} = $2 if (/^\s*(\S+)\s+(\S+)/);
}
untie %DB;
close(TXT);
</pre></div>
<div class="example"><p><code>
$ txt2dbm map.txt map.db
</code></p></div>
</li>
<li>
<strong>内部函数</strong><br />
MapType: <code>int</code>, MapSource: 内部的Apache函数
<p>这里的源是一个内部的Apache函数。
目前,还不能由你自己建立,只能使用下列已经存在的函数:</p>
<ul>
<li><strong>toupper</strong>:<br />
转换查找关键词为大写.</li>
<li><strong>tolower</strong>:<br />
转换查找关键词为小写.</li>
<li><strong>escape</strong>:<br />
转换查找关键词中的特殊字符为十六进制编码.</li>
<li><strong>unescape</strong>:<br />
转换查找关键词中的十六进制编码为特殊字符.</li>
</ul>
</li>
<li>
<strong>外部的重写程序</strong><br />
MapType: <code>prg</code>, MapSource: 有效的Unix文件系统文件名
<p>这里的源是一个程序,而不是一个映射表文件。
程序的编制语言可以随意选择,但最终结果必须是可执行的
(<em>即</em>, 或者是目标代码,或者是首行为'<code>#!/path/to/interpreter</code>'的脚本).</p>
<p>此程序仅在Apache服务器启动时启动一次,
随后通过<code>stdin</code>和<code>stdout</code>文件句柄与重写引擎交互。
对每个映射函数的查找操作,它从<code>stdin</code>接收以回车结束的查找关键词,
然后把查找结果以回车结束反馈到<code>stdout</code>,
如果查找失败,则返回四个字符的``<code>NULL</code>''
(<em>即</em>, 对给定的关键词没有对应的值)。
此程序的最简单形式是一个1:1的映射(<em>即</em>,key == value),如:</p>
<div class="example"><pre>
#!/usr/bin/perl
$| = 1;
while (<STDIN>) {
# ...put here any transformations or lookups...
print $_;
}
</pre></div>
<p>但是必须注意:</p>
<ol>
<li>``<em>即使它看来简单而愚蠢,只要正确,就保持原样(Keep it simple, stupid)</em>'' (KISS),
因为,在规则起作用时,此程序的崩溃会直接导致Apache服务器的崩溃。</li>
<li>避免犯一个常见的错误: 绝不要对<code>stdout</code>做缓冲I/O!
它会导致死循环! 所以上述例子中才会有``<code>$|=1</code>''...</li>
<li>使用<code class="directive"><a href="#rewritelock">RewriteLock</a></code>指令定义一个加锁文件,
用于同步mod_rewrite和此程序之间的通讯。缺省时是没有同步操作的。</li>
</ol>
</li>
</ul>
<p><code class="directive">RewriteMap</code>指令允许多次出现。
对每个映射函数都可以使用一个<code class="directive">RewriteMap</code>指令来定义其重写映射表。
虽然不能在目录的上下文中<strong>定义</strong>映射表,
但是,完全可以在其中<strong>使用</strong>映射表。</p>
<div class="note"><h3>注意</h3>对于纯文本和DBM格式的文件,已经查找过的关键词会被缓存在内核中,
直到映射表的<code>mtime</code>改变了或者服务器重启了。
这样,你可以把每个请求都会用到的映射函数放在规则中,这是没有问题的,因为外部查找只进行一次!
</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="RewriteOptions" id="RewriteOptions">RewriteOptions</a> <a name="rewriteoptions" id="rewriteoptions">指令</a></h2>
<table 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>RewriteOptions <var>Options</var></code></td></tr>
<tr><th><a href="directive-dict.html#Default">默认值:</a></th><td><code>RewriteOptions MaxRedirects=10</code></td></tr>
<tr><th><a href="directive-dict.html#Context">上下文:</a></th><td>服务器配置, 虚拟主机, 目录, .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>Extension</td></tr>
<tr><th><a href="directive-dict.html#Module">模块:</a></th><td>mod_rewrite</td></tr>
<tr><th><a href="directive-dict.html#Compatibility">兼容性:</a></th><td><code>MaxRedirects</code>在Apache 2.0.45以及更新的版本中有效</td></tr>
</table>
<p><code class="directive">RewriteOptions</code>指令为当前服务器级和目录级的配置设置一些选项。
<em>Option</em>可以是下列值之一:</p>
<dl>
<dt><code>inherit</code></dt>
<dd>此值强制当前配置可以继承其父配置。
在虚拟主机级配置中,它意味着主服务器的映射表、条件和规则可以被继承。
在目录级配置中,它意味着其父目录的<code>.htaccess</code>中的条件和规则可以被继承。</dd>
<dt><code>MaxRedirects=<var>number</var></code></dt>
<dd>为了避免目录级<code class="directive"><a href="#rewriterule">RewriteRule</a></code>的无休止的内部重定向,
在此类重定向和500内部服务器错误次数达到一个最大值的时候,
<code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>会停止对此请求的处理。
如果你确实需要对每个请求允许大于10次的内部重定向,可以增大这个值。</dd>
</dl>
</div>
<div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="directive-section"><h2><a name="RewriteRule" id="RewriteRule">RewriteRule</a> <a name="rewriterule" id="rewriterule">指令</a></h2>
<table 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>RewriteRule
<em>Pattern</em> <em>Substitution</em></code></td></tr>
<tr><th><a href="directive-dict.html#Context">上下文:</a></th><td>服务器配置, 虚拟主机, 目录, .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>Extension</td></tr>
<tr><th><a href="directive-dict.html#Module">模块:</a></th><td>mod_rewrite</td></tr>
<tr><th><a href="directive-dict.html#Compatibility">兼容性:</a></th><td>cookie-flag在Apache 2.0.40及其更新版本中有效.</td></tr>
</table>
<p><code class="directive">RewriteRule</code>指令是重写引擎的根本。此指令可以多次使用。
每个指令定义一个简单的重写规则。这些规则的<strong>定义顺序</strong>尤为<strong>重要</strong>,
因为,在运行时刻,规则是按这个顺序逐一生效的.</p>
<p><a id="patterns" name="patterns"><em>Pattern</em></a>是一个作用于当前URL的兼容perl的<a id="regexp" name="regexp">正则表达式</a>. 这里的``当前''是指该规则生效时的URL的值。
它可能与被请求的URL不同,因为其他规则可能在此之前已经发生匹配并对它做了改动。</p>
<p>正则表达式的一些用法:</p>
<div class="note"><pre>
<strong>Text:</strong>
<strong><code>.</code></strong> Any single character
<strong><code>[</code></strong>chars<strong><code>]</code></strong> Character class: One of chars
<strong><code>[^</code></strong>chars<strong><code>]</code></strong> Character class: None of chars
text1<strong><code>|</code></strong>text2 Alternative: text1 or text2
<strong>Quantifiers:</strong>
<strong><code>?</code></strong> 0 or 1 of the preceding text
<strong><code>*</code></strong> 0 or N of the preceding text (N > 0)
<strong><code>+</code></strong> 1 or N of the preceding text (N > 1)
<strong>Grouping:</strong>
<strong><code>(</code></strong>text<strong><code>)</code></strong> Grouping of text
(either to set the borders of an alternative or
for making backreferences where the <strong>N</strong>th group can
be used on the RHS of a RewriteRule with <code>$</code><strong>N</strong>)
<strong>Anchors:</strong>
<strong><code>^</code></strong> Start of line anchor
<strong><code>$</code></strong> End of line anchor
<strong>Escaping:</strong>
<strong><code>\</code></strong>char escape that particular char
(for instance to specify the chars "<code>.[]()</code>" <em>etc.</em>)
</pre></div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -