📄 mod_rewrite.html.en
字号:
<font size="-1">Request: /xyz/oldstuff.htmlInternal Processing: /xyz/oldstuff.html -> /abc/def/oldstuff.html (per-server Alias) /abc/def/oldstuff.html -> /abc/def/newstuff.html (per-dir RewriteRule) /abc/def/newstuff.html -> /xyz/newstuff.html (per-dir RewriteBase) /xyz/newstuff.html -> /abc/def/newstuff.html (per-server Alias)Result: /abc/def/newstuff.html</font></pre> <font size="-1">This seems very complicated but is the correct Apache internal processing, because the per-directory rewriting comes too late in the process. So, when it occurs the (rewritten) request has to be re-injected into the Apache kernel! BUT: While this seems like a serious overhead, it really isn't, because this re-injection happens fully internally to the Apache server and the same procedure is used by many other operations inside Apache. So, you can be sure the design and implementation is correct.</font> </td> </tr> </table> </blockquote> <hr noshade="noshade" size="1" /> <h3><a id="RewriteCond" name="RewriteCond">RewriteCond</a></h3> <a href="directive-dict.html#Syntax" rel="Help"><strong>Syntax:</strong></a> RewriteCond <em>TestString</em> <em>CondPattern</em><br /> <a href="directive-dict.html#Default" rel="Help"><strong>Default:</strong></a> <em>None</em><br /> <a href="directive-dict.html#Context" rel="Help"><strong>Context:</strong></a> server config, virtual host, 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 (partially), Apache 1.3<br /> <p>The <code>RewriteCond</code> directive defines a rule condition. Precede a <code>RewriteRule</code> directive with one or more <code>RewriteCond</code> directives. The following rewriting rule is only used if its pattern matches the current state of the URI <strong>and</strong> if these additional conditions apply too.</p> <p><em>TestString</em> is a string which can contains the following expanded constructs in addition to plain text:</p> <ul> <li> <strong>RewriteRule backreferences</strong>: These are backreferences of the form <blockquote> <strong><code>$N</code></strong> </blockquote> (0 <= N <= 9) which provide access to the grouped parts (parenthesis!) of the pattern from the corresponding <code>RewriteRule</code> directive (the one following the current bunch of <code>RewriteCond</code> directives). </li> <li> <strong>RewriteCond backreferences</strong>: These are backreferences of the form <blockquote> <strong><code>%N</code></strong> </blockquote> (1 <= N <= 9) which provide access to the grouped parts (parentheses!) of the pattern from the last matched <code>RewriteCond</code> directive in the current bunch of conditions. </li> <li> <strong>RewriteMap expansions</strong>: These are expansions of the form <blockquote> <strong><code>${mapname:key|default}</code></strong> </blockquote> See <a href="#mapfunc">the documentation for RewriteMap</a> for more details. </li> <li> <strong>Server-Variables</strong>: These are variables of the form <blockquote> <strong><code>%{</code> <em>NAME_OF_VARIABLE</em> <code>}</code></strong> </blockquote> where <em>NAME_OF_VARIABLE</em> can be a string taken from the following list: <table bgcolor="#F0F0F0" cellspacing="0" cellpadding="5"> <tr> <td valign="TOP"> <strong>HTTP headers:</strong> <p><font size="-1">HTTP_USER_AGENT<br /> HTTP_REFERER<br /> HTTP_COOKIE<br /> HTTP_FORWARDED<br /> HTTP_HOST<br /> HTTP_PROXY_CONNECTION<br /> HTTP_ACCEPT<br /> </font></p> </td> <td valign="TOP"> <strong>connection & request:</strong> <p><font size="-1">REMOTE_ADDR<br /> REMOTE_HOST<br /> REMOTE_USER<br /> REMOTE_IDENT<br /> REQUEST_METHOD<br /> SCRIPT_FILENAME<br /> PATH_INFO<br /> QUERY_STRING<br /> AUTH_TYPE<br /> </font></p> </td> </tr> <tr> <td valign="TOP"> <strong>server internals:</strong> <p><font size="-1">DOCUMENT_ROOT<br /> SERVER_ADMIN<br /> SERVER_NAME<br /> SERVER_ADDR<br /> SERVER_PORT<br /> SERVER_PROTOCOL<br /> SERVER_SOFTWARE<br /> </font></p> </td> <td valign="TOP"> <strong>system stuff:</strong> <p><font size="-1">TIME_YEAR<br /> TIME_MON<br /> TIME_DAY<br /> TIME_HOUR<br /> TIME_MIN<br /> TIME_SEC<br /> TIME_WDAY<br /> TIME<br /> </font></p> </td> <td valign="TOP"> <strong>specials:</strong> <p><font size="-1">API_VERSION<br /> THE_REQUEST<br /> REQUEST_URI<br /> REQUEST_FILENAME<br /> IS_SUBREQ<br /> </font></p> </td> </tr> </table> <table width="70%" border="0" bgcolor="#E0E0F0" cellspacing="0" cellpadding="10"> <tr> <td> <p><strong>Notice:</strong> These variables all correspond to the similarly named HTTP MIME-headers, C variables of the Apache server or <code>struct tm</code> fields of the Unix system. Most are documented elsewhere in the Manual or in the CGI specification. Those that are special to mod_rewrite include:</p> <dl> <dt><code>IS_SUBREQ</code></dt> <dd>Will contain the text "true" if the request currently being processed is a sub-request, "false" otherwise. Sub-requests may be generated by modules that need to resolve additional files or URIs in order to complete their tasks.</dd> <dt><code>API_VERSION</code></dt> <dd>This is the version of the Apache module API (the internal interface between server and module) in the current httpd build, as defined in include/ap_mmn.h. The module API version corresponds to the version of Apache in use (in the release version of Apache 1.3.14, for instance, it is 19990320:10), but is mainly of interest to module authors.</dd> <dt><code>THE_REQUEST</code></dt> <dd>The full HTTP request line sent by the browser to the server (e.g., "<code>GET /index.html HTTP/1.1</code>"). This does not include any additional headers sent by the browser.</dd> <dt><code>REQUEST_URI</code></dt> <dd>The resource requested in the HTTP request line. (In the example above, this would be "/index.html".)</dd> <dt><code>REQUEST_FILENAME</code></dt> <dd>The full local filesystem path to the file or script matching the request.</dd> </dl> </td> </tr> </table> </li> </ul> <p>Special Notes:</p> <ol> <li>The variables SCRIPT_FILENAME and REQUEST_FILENAME contain the same value, <em>i.e.</em>, the value of the <code>filename</code> field of the internal <code>request_rec</code> structure of the Apache server. The first name is just the commonly known CGI variable name while the second is the consistent counterpart to REQUEST_URI (which contains the value of the <code>uri</code> field of <code>request_rec</code>).</li> <li>There is the special format: <code>%{ENV:variable}</code> where <em>variable</em> can be any environment variable. This is looked-up via internal Apache structures and (if not found there) via <code>getenv()</code> from the Apache server process.</li> <li>There is the special format: <code>%{HTTP:header}</code> where <em>header</em> can be any HTTP MIME-header name. This is looked-up from the HTTP request. Example: <code>%{HTTP:Proxy-Connection}</code> is the value of the HTTP header ``<code>Proxy-Connection:</code>''.</li> <li>There is the special format <code>%{LA-U:variable}</code> for look-aheads which perform an internal (URL-based) sub-request to determine the final value of <em>variable</em>. Use this when you want to use a variable for rewriting which is actually set later in an API phase and thus is not available at the current stage. For instance when you want to rewrite according to the <code>REMOTE_USER</code> variable from within the per-server context (<code>httpd.conf</code> file) you have to use <code>%{LA-U:REMOTE_USER}</code> because this variable is set by the authorization phases which come <em>after</em> the URL translation phase where mod_rewrite operates. On the other hand, because mod_rewrite implements its per-directory context (<code>.htaccess</code> file) via the Fixup phase of the API and because the authorization phases come <em>before</em> this phase, you just can use <code>%{REMOTE_USER}</code> there.</li> <li>There is the special format: <code>%{LA-F:variable}</code> which performs an internal (filename-based) sub-request to determine the final value of <em>variable</em>. Most of the time this is the same as LA-U above.</li> </ol> <p><em>CondPattern</em> is the condition pattern, <em>i.e.</em>, a regular expression which is applied to the current instance of the <em>TestString</em>, <em>i.e.</em>, <em>TestString</em> is evaluated and then matched against <em>CondPattern</em>.</p> <p><strong>Remember:</strong> <em>CondPattern</em> is a standard <em>Extended Regular Expression</em> with some additions:</p> <ol> <li>You can prefix the pattern string with a '<code>!</code>' character (exclamation mark) to specify a <strong>non</strong>-matching pattern.</li> <li> There are some special variants of <em>CondPatterns</em>. Instead of real regular expression strings you can also use one of the following: <ul> <li>'<strong><CondPattern</strong>' (is lexically lower)<br /> Treats the <em>CondPattern</em> as a plain string and compares it lexically to <em>TestString</em>. True if <em>TestString</em> is lexically lower than <em>CondPattern</em>.</li> <li>'<strong>>CondPattern</strong>' (is lexically greater)<br /> Treats the <em>CondPattern</em> as a plain string and compares it lexically to <em>TestString</em>. True if <em>TestString</em> is lexically greater than <em>CondPattern</em>.</li> <li>'<strong>=CondPattern</strong>' (is lexically equal)<br /> Treats the <em>CondPattern</em> as a plain string and
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -