mod_rewrite.html.en
来自「apache的软件linux版本」· EN 代码 · 共 1,362 行 · 第 1/5 页
EN
1,362 行
<div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="directive-section"><h2><a name="RewriteMap" id="RewriteMap">RewriteMap</a> <a name="rewritemap" id="rewritemap">Directive</a></h2><table class="directive"><tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Defines a mapping function for key-lookup</td></tr><tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>RewriteMap <em>MapName</em> <em>MapType</em>:<em>MapSource</em></code></td></tr><tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config, virtual host</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>The choice of different dbm types is available inApache 2.0.41 and later</td></tr></table> <p>The <code class="directive">RewriteMap</code> directive defines a <em>Rewriting Map</em> which can be used inside rule substitution strings by the mapping-functions to insert/substitute fields through a key lookup. The source of this lookup can be of various types.</p> <p>The <a id="mapfunc" name="mapfunc"><em>MapName</em></a> is the name of the map and will be used to specify a mapping-function for the substitution strings of a rewriting rule via one of the following constructs:</p> <p class="indent"> <strong><code>${</code> <em>MapName</em> <code>:</code> <em>LookupKey</em> <code>}</code><br /> <code>${</code> <em>MapName</em> <code>:</code> <em>LookupKey</em> <code>|</code> <em>DefaultValue</em> <code>}</code></strong> </p> <p>When such a construct occurs, the map <em>MapName</em> is consulted and the key <em>LookupKey</em> is looked-up. If the key is found, the map-function construct is substituted by <em>SubstValue</em>. If the key is not found then it is substituted by <em>DefaultValue</em> or by the empty string if no <em>DefaultValue</em> was specified.</p> <p>For example, you might define a <code class="directive">RewriteMap</code> as:</p> <div class="example"><p><code> RewriteMap examplemap txt:/path/to/file/map.txt </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 ommitted, the compile-time default will be chosen. You can create such a file with any DBM tool or with the following Perl script. Be sure to adjust it to create the appropriate type of DBM. The example creates an NDBM file.</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>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><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#Default">Default:</a></th><td><code>RewriteOptions MaxRedirects=10</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 available in Apache 2.0.45 and
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?