📄 mod_rewrite.html.en
字号:
<table border="0" cellspacing="1" cellpadding="5" bgcolor="#F0F0F0"> <tr> <td><pre>#### map.txt -- rewriting map##Ralf.S.Engelschall rse # Bastard Operator From HellMr.Joe.Average joe # Mr. Average</pre> </td> </tr> </table> <table border="0" cellspacing="1" cellpadding="5" bgcolor="#F0F0F0"> <tr> <td><pre>RewriteMap real-to-user txt:/path/to/file/map.txt</pre> </td> </tr> </table> </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. Although this sounds crazy and useless, it was actually designed for load balancing in a reverse proxy situation where the looked up values are server names. Example:</p> <table border="0" cellspacing="1" cellpadding="5" bgcolor="#F0F0F0"> <tr> <td><pre>#### map.txt -- rewriting map##static www1|www2|www3|www4dynamic www5|www6</pre> </td> </tr> </table> <table border="0" cellspacing="1" cellpadding="5" bgcolor="#F0F0F0"> <tr> <td><pre>RewriteMap servers rnd:/path/to/file/map.txt</pre> </td> </tr> </table> </li> <li> <strong>Hash File</strong><br /> MapType: <code>dbm</code>, MapSource: Unix filesystem path to valid regular file <p>Here the source is a binary NDBM format 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. You can create such a file with any NDBM tool or with the following Perl script:</p> <table border="0" cellspacing="1" cellpadding="5" bgcolor="#F0F0F0"> <tr> <td><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> </td> </tr> </table> <table border="0" cellspacing="1" cellpadding="5" bgcolor="#F0F0F0"> <tr> <td><pre>$ txt2dbm map.txt map.db</pre> </td> </tr> </table> </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> 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> <table border="0" cellspacing="1" cellpadding="5" bgcolor="#F0F0F0"> <tr> <td><pre>#!/usr/bin/perl$| = 1;while (<STDIN>) { # ...put here any transformations or lookups... print $_;}</pre> </td> </tr> </table> <p>But be very careful:<br /> </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 <samp>RewriteLock</samp> 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> The <code>RewriteMap</code> directive can occur more than once. For each mapping-function use one <code>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. <table width="70%" border="0" bgcolor="#E0E0F0" cellspacing="0" cellpadding="10"> <tr> <td><strong>Note:</strong> 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!</td> </tr> </table> <hr noshade="noshade" size="1" /> <h3><a id="RewriteBase" name="RewriteBase">RewriteBase</a></h3> <a href="directive-dict.html#Syntax" rel="Help"><strong>Syntax:</strong></a> RewriteBase <em>URL-path</em><br /> <a href="directive-dict.html#Default" rel="Help"><strong>Default:</strong></a> <em>default is the physical directory path</em><br /> <a href="directive-dict.html#Context" rel="Help"><strong>Context:</strong></a> directory, .htaccess<br /> <a href="directive-dict.html#Override" rel="Help"><strong>Override:</strong></a> <em>FileInfo</em><br /> <a href="directive-dict.html#Status" rel="Help"><strong>Status:</strong></a> Extension<br /> <a href="directive-dict.html#Module" rel="Help"><strong>Module:</strong></a> mod_rewrite.c<br /> <a href="directive-dict.html#Compatibility" rel="Help"><strong>Compatibility:</strong></a> Apache 1.2<br /> <p>The <code>RewriteBase</code> directive explicitly sets the base URL for per-directory rewrites. As you will see below, <code>RewriteRule</code> can be used in per-directory config files (<code>.htaccess</code>). There it will act locally, <em>i.e.</em>, the local directory prefix is stripped at this stage of processing and your rewriting rules act only on the remainder. At the end it is automatically added back to the path.</p> <p>When a substitution occurs for a new URL, this module has to re-inject the URL into the server processing. To be able to do this it needs to know what the corresponding URL-prefix or URL-base is. By default this prefix is the corresponding filepath itself. <strong>But at most websites URLs are NOT directly related to physical filename paths, so this assumption will usually be wrong!</strong> There you have to use the <code>RewriteBase</code> directive to specify the correct URL-prefix.</p> <table width="70%" border="0" bgcolor="#E0E0F0" cellspacing="0" cellpadding="10"> <tr> <td><strong>Notice:</strong> If your webserver's URLs are <strong>not</strong> directly related to physical file paths, you have to use <code>RewriteBase</code> in every <code>.htaccess</code> files where you want to use <code>RewriteRule</code> directives.</td> </tr> </table> <p><strong>Example:</strong></p> <blockquote> Assume the following per-directory config file: <table border="0" cellspacing="1" cellpadding="5" bgcolor="#F0F0F0"> <tr> <td><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 <em>e.g.</em>#RewriteEngine On# let the server know that we were reached via /xyz and not# via the physical path prefix /abc/defRewriteBase /xyz# now the rewriting rulesRewriteRule ^oldstuff\.html$ newstuff.html</pre> </td> </tr> </table> <p>In the above example, a request to <code>/xyz/oldstuff.html</code> gets correctly rewritten to the physical file <code>/abc/def/newstuff.html</code>.</p> <table width="70%" border="0" bgcolor="#E0E0F0" cellspacing="0" cellpadding="10"> <tr> <td> <font size="-1"><strong>Note - For Apache hackers:</strong><br /> The following list gives detailed information about the internal processing steps:</font> <pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -