📄 mod_rewrite.html
字号:
}
untie %DB;
close(TXT);
</pre></div>
<div class="example"><p><code>
$ txt2dbm map.txt map.db
</code></p></div>
</li>
<li>
<strong>Internal Function</strong><br />
MapType: <code>int</code>, MapSource: Internal Apache
function
<p>Here the source is an internal Apache function.
Currently you cannot create your own, but the following
functions already exists:</p>
<ul>
<li><strong>toupper</strong>:<br />
Converts the looked up key to all upper case.</li>
<li><strong>tolower</strong>:<br />
Converts the looked up key to all lower case.</li>
<li><strong>escape</strong>:<br />
Translates special characters in the looked up key to
hex-encodings.</li>
<li><strong>unescape</strong>:<br />
Translates hex-encodings in the looked up key back to
special characters.</li>
</ul>
</li>
<li>
<strong>External Rewriting Program</strong><br />
MapType: <code>prg</code>, MapSource: Unix filesystem
path to valid regular file
<p>Here the source is a program, not a map file. To
create it you can use the language of your choice, but
the result has to be a executable (<em>i.e.</em>, either
object-code or a script with the magic cookie trick
'<code>#!/path/to/interpreter</code>' as the first
line).</p>
<p>This program is started once at startup of the Apache
servers and then communicates with the rewriting engine
over its <code>stdin</code>和<code>stdout</code>
file-handles. For each map-function lookup it will
receive the key to lookup as a newline-terminated string
on <code>stdin</code>. It then has to give back the
looked-up value as a newline-terminated string on
<code>stdout</code> or the four-character string
"<code>NULL</code>" if it fails (<em>i.e.</em>, there
is no corresponding value for the given key). A trivial
program which will implement a 1:1 map (<em>i.e.</em>,
key == value) could be:</p>
<div class="example"><pre>
#!/usr/bin/perl
$| = 1;
while (<STDIN>) {
# ...put here any transformations or lookups...
print $_;
}
</pre></div>
<p>But be very careful:</p>
<ol>
<li>"<em>Keep it simple, stupid</em>" (KISS), because
if this program hangs it will hang the Apache server
when the rule occurs.</li>
<li>Avoid one common mistake: never do buffered I/O on
<code>stdout</code>! This will cause a deadloop! Hence
the "<code>$|=1</code>" in the above example...</li>
<li>Use the <code class="directive"><a href="#rewritelock">RewriteLock</a></code> directive to
define a lockfile mod_rewrite can use to synchronize the
communication to the program. By default no such
synchronization takes place.</li>
</ol>
</li>
</ul>
<p><code class="directive">RewriteMap</code> directive can occur more than
once. For each mapping-function use one
<code class="directive">RewriteMap</code> directive to declare its rewriting
mapfile. While you cannot <strong>declare</strong> a map in
per-directory context it is of course possible to
<strong>use</strong> this map in per-directory context. </p>
<div class="note"><h3>注意</h3> For plain text and DBM format files the
looked-up keys are cached in-core until the <code>mtime</code> of the
mapfile changes or the server does a restart. This way you can have
map-functions in rules which are used for <strong>every</strong>
request. This is no problem, because the external lookup only happens
once!
</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 border="1" cellpadding="0" cellspacing="0" bordercolor="#AAAAAA" class="directive">
<tr><th><a href="directive-dict.html#Description">说明</a></th><td>Sets some special options for the rewrite engine</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#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>
<tr><th><a href="directive-dict.html#Compatibility">兼容性</a></th><td><code>MaxRedirects</code> is no longer available in version 2.1 及以后的版本中可用</td></tr>
</table>
<p><code class="directive">RewriteOptions</code> directive sets some
special options for the current per-server or per-directory
configuration. The <em>Option</em> string can be currently only one:</p>
<dl>
<dt><code>inherit</code></dt>
<dd>This forces the current configuration to inherit the
configuration of the parent. In per-virtual-server context
this means that the maps, conditions and rules of the main
server are inherited. In per-directory context this means
that conditions and rules of the parent directory's
<code>.htaccess</code> configuration are inherited.</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 border="1" cellpadding="0" cellspacing="0" bordercolor="#AAAAAA" class="directive">
<tr><th><a href="directive-dict.html#Description">说明</a></th><td>Defines rules for the rewriting engine</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>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>
<tr><th><a href="directive-dict.html#Compatibility">兼容性</a></th><td>The cookie-flag is available in Apache 2.0.40 及以后的版本中可用</td></tr>
</table>
<p><code class="directive">RewriteRule</code> directive is the real
rewriting workhorse. The directive can occur more than once.
Each directive then defines one single rewriting rule. The
<strong>definition order</strong> of these rules is
<strong>important</strong>, because this order is used when
applying the rules at run-time.</p>
<p><a id="patterns" name="patterns"><em>Pattern</em></a> is
a perl compatible <a id="regexp" name="regexp">regular
expression</a> which gets applied to the current URL. Here
"current" means the value of the URL when this rule gets
applied. This may not be the originally requested URL,
because any number of rules may already have matched and made
alterations to it.</p>
<p>Some hints about the syntax of <a class="glossarylink" href="../glossary.html#regex" title="see glossary">regular
expressions</a>:</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>
<p>For more information about regular expressions have a look at the
perl regular expression manpage ("<a href="http://www.perldoc.com/perl5.6.1/pod/perlre.html">perldoc
perlre</a>"). If you are interested in more detailed
information about regular expressions and their variants
(POSIX regex <em>etc.</em>) have a look at the
following dedicated book on this topic:</p>
<p class="indent">
<em>Mastering Regular Expressions, 2nd Edition</em><br />
Jeffrey E.F. Friedl<br />
O'Reilly & Associates, Inc. 2002<br />
ISBN 0-596-00289-0<br />
</p>
<p>Additionally in mod_rewrite the NOT character
('<code>!</code>') is a possible pattern prefix. This gives
you the ability to negate a pattern; to say, for instance:
"<em>if the current URL does <strong>NOT</strong> match this
pattern</em>". This can be used for exceptional cases, where
it is easier to match the negative pattern, or as a last
default rule.</p>
<div class="note"><h3>Notice</h3>
When using the NOT character
to negate a pattern you cannot have grouped wildcard
parts in the pattern. This is impossible because when the
pattern does NOT match, there are no contents for the
groups. In consequence, if negated patterns are used, you
cannot use <code>$N</code> in the substitution
string!
</div>
<p><a id="rhs" name="rhs"><em>Substitution</em></a> of a
rewriting rule is the string which is substituted for (or
replaces) the original URL for which <em>Pattern</em>
matched. Beside plain text you can use</p>
<ol>
<li>back-references <code>$N</code> to the RewriteRule
pattern</li>
<li>back-references <code>%N</code> to the last matched
RewriteCond pattern</li>
<li>server-variables as in rule condition test-strings
(<code>%{VARNAME}</code>)</li>
<li><a href="#mapfunc">mapping-function</a> calls
(<code>${mapname:key|default}</code>)</li>
</ol>
<p>Back-references are <code>$</code><strong>N</strong>
(<strong>N</strong>=0..9) identifiers which will be replaced
by the contents of the <strong>N</strong>th group of the
matched <em>Pattern</em>. The server-variables are the same
as for the <em>TestString</em> of a <code>RewriteCond</code>
directive. The mapping-functions come from the
<code>RewriteMap</code> directive and are explained there.
These three types of variables are expanded in the order of
the above list. </p>
<p>As already mentioned above, all the rewriting rules are
applied to the <em>Substitution</em> (in the order of
definition in the config file). The URL is <strong>completely
replaced</strong> by the <em>Substitution</em> and the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -