📄 mod_rewrite.html.en
字号:
</code></p></div> <p>You would then be able to use this map in a <code class="directive">RewriteRule</code> as follows:</p> <div class="example"><p><code> RewriteRule ^/ex/(.*) ${examplemap:$1} </code></p></div> <p>The following combinations for <em>MapType</em> and <em>MapSource</em> can be used:</p> <ul> <li> <strong>Standard Plain Text</strong><br /> MapType: <code>txt</code>, MapSource: Unix filesystem path to valid regular file <p>This is the standard rewriting map feature where the <em>MapSource</em> is a plain ASCII file containing either blank lines, comment lines (starting with a '#' character) or pairs like the following - one per line.</p> <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 HellMr.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>Randomized Plain Text</strong><br /> MapType: <code>rnd</code>, MapSource: Unix filesystem path to valid regular file <p>This is identical to the Standard Plain Text variant above but with a special post-processing feature: After looking up a value it is parsed according to contained ``<code>|</code>'' characters which have the meaning of ``or''. In other words they indicate a set of alternatives from which the actual returned value is chosen randomly. For example, you might use the following map file and directives to provide a random load balancing between several back-end server, via a reverse-proxy. Images are sent to one of the servers in the 'static' pool, while everything else is sent to one of the 'dynamic' pool.</p> <p>Example:</p><div class="example"><h3>Rewrite map file</h3><pre>#### map.txt -- rewriting map##static www1|www2|www3|www4dynamic www5|www6</pre></div><div class="example"><h3>Configuration directives</h3><p><code>RewriteMap servers rnd:/path/to/file/map.txt<br /><br />RewriteRule ^/(.*\.(png|gif|jpg)) http://${servers:static}/$1[NC,P,L]<br />RewriteRule ^/(.*) http://${servers:dynamic}/$1 [P,L]</code></p></div> </li> <li> <strong>Hash File</strong><br /> MapType: <code>dbm[=<em>type</em>]</code>, MapSource: Unix filesystem path to valid regular file <p>Here the source is a binary format DBM file containing the same contents as a <em>Plain Text</em> format file, but in a special representation which is optimized for really fast lookups. The <em>type</em> can be sdbm, gdbm, ndbm, or db depending on <a href="../install.html#dbm">compile-time settings</a>. If the <em>type</em> is omitted, the compile-time default will be chosen.</p> <p>To create a dbm file from a source text file, use the <a href="../programs/httxt2dbm.html">httxt2dbm</a> utility.</p><div class="example"><p><code>$ httxt2dbm -i mapfile.txt -o mapfile.map</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 exist:</p> <ul> <li><strong>toupper</strong>:<br /> Converts the key to all upper case.</li> <li><strong>tolower</strong>:<br /> Converts the key to all lower case.</li> <li><strong>escape</strong>:<br /> Translates special characters in the key to hex-encodings.</li> <li><strong>unescape</strong>:<br /> Translates hex-encodings in the 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 a language of your choice, but the result has to be an executable program (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, when the Apache server is started, and then communicates with the rewriting engine via its <code>stdin</code> and <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> <p>External rewriting programs are not started if they're defined in a context that does not have <code class="directive">RewriteEngine</code> set to <code>on</code></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). If this program hangs, it will cause Apache to hang when trying to use the relevant rewrite rule.</li> <li>A common mistake is to use buffered I/O on <code>stdout</code>. Avoid this, as it will cause a deadloop! ``<code>$|=1</code>'' is used above, to prevent this.</li> <li>The <code class="directive"><a href="#rewritelock">RewriteLock</a></code> directive can be used to define a lockfile which mod_rewrite can use to synchronize communication with the mapping program. By default no such synchronization takes place.</li> </ol> </li> </ul> <p>The <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>Note</h3> For plain text and DBM format files thelooked-up keys are cached in-core until the <code>mtime</code> of themapfile changes or the server does a restart. This way you can havemap-functions in rules which are used for <strong>every</strong>request. This is no problem, because the external lookup only happensonce!</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">Directive</a></h2><table class="directive"><tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Sets some special options for the rewrite engine</td></tr><tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>RewriteOptions <var>Options</var></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><tr><th><a href="directive-dict.html#Compatibility">Compatibility:</a></th><td><code>MaxRedirects</code> is no longer available in version 2.1 andlater</td></tr></table> <p>The <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 currently only be one of the following:</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">Directive</a></h2><table class="directive"><tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Defines rules for the rewriting engine</td></tr><tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>RewriteRule <em>Pattern</em> <em>Substitution</em> [<em>flags</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">RewriteRule</code> directive is the real rewriting workhorse. The directive can occur more than once, with each instance defining a single rewrite rule. The order in which these rules are defined is important - this is the order in which they will be applied 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>. On the first RewriteRule it is applied to the <a href="./directive-dict.html#Syntax">URL-path</a> of the request; subsequent patterns are applied to the output of the last matched RewriteRule.</p><div class="note"><h3>What is matched?</h3> <p>The <em>Pattern</em> will initially be matched against the part of the URL after the hostname and port, and before the query string. If you wish to match against the hostname, port, or query string, use a <code class="directive"><a href="#rewritecond">RewriteCond</a></code> with the <code>%{HTTP_HOST}</code>, <code>%{SERVER_PORT}</code>, or <code>%{QUERY_STRING}</code> variables respectively.</p></div> <p>For some hints on <a class="glossarylink" href="../glossary.html#regex" title="see glossary">regular expressions</a>, see the <a href="../rewrite/rewrite_intro.html#regex">mod_rewrite Introduction</a>.</p> <p>In mod_rewrite, the NOT character ('<code>!</code>') is also available as a possible pattern prefix. This enables you 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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -