📄 mod_rewrite.html.en
字号:
compares it lexically to <em>TestString</em>. True if <em>TestString</em> is lexically equal to <em>CondPattern</em>, i.e the two strings are exactly equal (character by character). If <em>CondPattern</em> is just <samp>""</samp> (two quotation marks) this compares <em>TestString</em> to the empty string.</li> <li>'<strong>-d</strong>' (is <strong>d</strong>irectory)<br /> Treats the <em>TestString</em> as a pathname and tests if it exists and is a directory.</li> <li>'<strong>-f</strong>' (is regular <strong>f</strong>ile)<br /> Treats the <em>TestString</em> as a pathname and tests if it exists and is a regular file.</li> <li>'<strong>-s</strong>' (is regular file with <strong>s</strong>ize)<br /> Treats the <em>TestString</em> as a pathname and tests if it exists and is a regular file with size greater than zero.</li> <li>'<strong>-l</strong>' (is symbolic <strong>l</strong>ink)<br /> Treats the <em>TestString</em> as a pathname and tests if it exists and is a symbolic link.</li> <li>'<strong>-F</strong>' (is existing file via subrequest)<br /> Checks if <em>TestString</em> is a valid file and accessible via all the server's currently-configured access controls for that path. This uses an internal subrequest to determine the check, so use it with care because it decreases your servers performance!</li> <li>'<strong>-U</strong>' (is existing URL via subrequest)<br /> Checks if <em>TestString</em> is a valid URL and accessible via all the server's currently-configured access controls for that path. This uses an internal subrequest to determine the check, so use it with care because it decreases your server's performance!</li> </ul> <table width="70%" border="0" bgcolor="#E0E0F0" cellspacing="0" cellpadding="10"> <tr> <td><strong>Notice:</strong> All of these tests can also be prefixed by an exclamation mark ('!') to negate their meaning.</td> </tr> </table> </li> </ol> <p>Additionally you can set special flags for <em>CondPattern</em> by appending</p> <blockquote> <strong><code>[</code><em>flags</em><code>]</code></strong> </blockquote> as the third argument to the <code>RewriteCond</code> directive. <em>Flags</em> is a comma-separated list of the following flags: <ul> <li>'<strong><code>nocase|NC</code></strong>' (<strong>n</strong>o <strong>c</strong>ase)<br /> This makes the test case-insensitive, <em>i.e.</em>, there is no difference between 'A-Z' and 'a-z' both in the expanded <em>TestString</em> and the <em>CondPattern</em>. This flag is effective only for comparisons between <em>TestString</em> and <em>CondPattern</em>. It has no effect on filesystem and subrequest checks.</li> <li> '<strong><code>ornext|OR</code></strong>' (<strong>or</strong> next condition)<br /> Use this to combine rule conditions with a local OR instead of the implicit AND. Typical example: <blockquote><pre>RewriteCond %{REMOTE_HOST} ^host1.* [OR]RewriteCond %{REMOTE_HOST} ^host2.* [OR]RewriteCond %{REMOTE_HOST} ^host3.*RewriteRule ...some special stuff for any of these hosts...</pre> </blockquote> Without this flag you would have to write the cond/rule three times. </li> </ul> <p><strong>Example:</strong></p> <blockquote> To rewrite the Homepage of a site according to the ``<code>User-Agent:</code>'' header of the request, you can use the following: <blockquote><pre>RewriteCond %{HTTP_USER_AGENT} ^Mozilla.*RewriteRule ^/$ /homepage.max.html [L]RewriteCond %{HTTP_USER_AGENT} ^Lynx.*RewriteRule ^/$ /homepage.min.html [L]RewriteRule ^/$ /homepage.std.html [L]</pre> </blockquote> Interpretation: If you use Netscape Navigator as your browser (which identifies itself as 'Mozilla'), then you get the max homepage, which includes Frames, <em>etc.</em> If you use the Lynx browser (which is Terminal-based), then you get the min homepage, which contains no images, no tables, <em>etc.</em> If you use any other browser you get the standard homepage. </blockquote> <hr noshade="noshade" size="1" /> <h3><a id="RewriteRule" name="RewriteRule">RewriteRule</a></h3> <a href="directive-dict.html#Syntax" rel="Help"><strong>Syntax:</strong></a> RewriteRule <em>Pattern</em> <em>Substitution</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>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> can be (for Apache 1.1.x a System V8 and for Apache 1.2.x and later a POSIX) <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 regular expressions:</p> <table bgcolor="#F0F0F0" cellspacing="0" cellpadding="5"> <tr> <td valign="TOP"><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> </td> </tr> </table> <p>For more information about regular expressions either have a look at your local regex(3) manpage or its <code>src/regex/regex.3</code> copy in the Apache 1.3 distribution. If you are interested in more detailed information about regular expressions and their variants (POSIX regex, Perl regex, <em>etc.</em>) have a look at the following dedicated book on this topic:</p> <blockquote> <em>Mastering Regular Expressions</em><br /> Jeffrey E.F. Friedl<br /> Nutshell Handbook Series<br /> O'Reilly & Associates, Inc. 1997<br /> ISBN 1-56592-257-3<br /> </blockquote> <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> <table width="70%" border="0" bgcolor="#E0E0F0" cellspacing="0" cellpadding="10"> <tr> <td><strong>Notice:</strong> 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!</td> </tr> </table> <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> 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>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 rewriting process goes on until there are no more rules unless explicitly terminated by a <code><strong>L</strong></code> flag - see below.</p> <p>There is a special substitution string named '<code>-</code>' which means: <strong>NO substitution</strong>! Sounds silly? No, it is useful to provide rewriting rules which <strong>only</strong> match some URLs but do no substitution, <em>e.g.</em>, in conjunction with the <strong>C</strong> (chain) flag to be able to have more than one pattern to be applied before a substitution occurs.</p> <p>One more note: You can even create URLs in the substitution string containing a query string part. Just use a question mark inside the substitution string to indicate that the following stuff should be re-injected into the QUERY_STRING. When you want to erase an existing query string, end the substitution string with just the question mark.</p> <table width="70%" border="0" bgcolor="#E0E0F0" cellspacing="0" cellpadding="10"> <tr> <td><strong>Note</strong>: There is a special feature: When you prefix a substitution field with <code>http://</code><em>thishost</em>[<em>:thisport</em>] then <strong>mod_rewrite</strong> automatically strips it out. This auto-reduction on implicit external redirect URLs is a useful and important feature when used in combination with a mapping-function which generates the hostname part. Have a look at the first example in the example section below to understand this.</td> </tr> </table> <table width="70%" border="0" bgcolor="#E0E0F0" cellspacing="0" cellpadding="10"> <tr> <td><strong>Remember:</strong> An unconditional external redirect to your own server will not work with the prefix <code>http://thishost</code> because of this feature. To achieve such a self-redirect, you have to use the <strong>R</strong>-flag (see below).</td> </tr> </table> <p>Additionally you can set special flags for <em>Substitution</em> by appending</p> <blockquote> <strong><code>[</code><em>flags</em><code>]</code></strong> </blockquote> as the third argument to the <code>RewriteRule</code> directive. <em>Flags</em> is a comma-separated list of the following flags: <ul> <li> '<strong><code>redirect|R</code> [=<em>code</em>]</strong>' (force <a id="redirect" name="redirect"><strong>r</strong>edirect</a>)<br /> Prefix <em>Substitution</em> with
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -