📄 rewrite_flags.html.en
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!-- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX This file is generated from xml source: DO NOT EDIT XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX --><title>Apache mod_rewrite Flags - Apache HTTP Server</title><link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" /><link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" /><link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link href="../images/favicon.ico" rel="shortcut icon" /></head><body id="manual-page"><div id="page-header"><p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p><p class="apache">Apache HTTP Server Version 2.2</p><img alt="" src="../images/feather.gif" /></div><div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div><div id="path"><a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.2</a> > <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>Apache mod_rewrite Flags</h1><div class="toplang"><p><span>Available Languages: </span><a href="../en/rewrite/rewrite_flags.html" title="English"> en </a></p></div><p>This document discusses the flags which are available to the<code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> directive,providing detailed explanations and examples. This is not necessarilya comprehensive list of all flags available, so be sure to alsoconsult the reference documentation.</p></div><div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#introduction">Introduction</a></li><li><img alt="" src="../images/down.gif" /> <a href="#flags">The flags</a></li></ul><h3>See also</h3><ul class="seealso"><li><a href="../mod/mod_rewrite.html">Module documentation</a></li><li><a href="rewrite_tech.html">Technical details</a></li><li><a href="rewrite_guide.html">Rewrite Guide - useful examples</a></li><li><a href="rewrite_guide_advanced.html">Advanced Rewrite Guide -advanced useful examples</a></li></ul></div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="section"><h2><a name="introduction" id="introduction">Introduction</a></h2><p><code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code>s can havetheir behavior modified by one or more flags. Flags are included insquare brackets at the end of the rule, and multiple flags are separatedby commas.</p><div class="example"><p><code>RewriteRule pattern target [Flag1,Flag2,Flag3]</code></p></div><p>The flags all have a short form, such as <code>CO</code>, as well asa longer form, such as <code>cookie</code>. Some flags take one or morearguments. Flags are not case sensitive.</p></div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div><div class="section"><h2><a name="flags" id="flags">The flags</a></h2><p>Each flag has a long and short form. While it is most common to usethe short form, it is recommended that you familiarize yourself with thelong form, so that you remember what each flag is supposed to do.</p><p>Presented here are each of the available flags, along with an exampleof how you might use them.</p><h3><a name="flag_c" id="flag_c">C|chain</a></h3><p>The [C] or [chain] flag indicates that the <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> is chained to the nextrule. That is, if the rule matches, then it is processed as usual andcontrol moves on to the next rule. However, if it does not match, thenthe next rule, and any other rules that are chained together, will beskipped.</p><h3><a name="flag_co" id="flag_co">CO|cookie</a></h3><p>The [CO], or [cookie] flag, allows you to set a cookie when aparticular <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code>matches. The argument consists of three required fields and two optionalfields.</p><p>You must declare a name and value for the cookie to be set, and thedomain for which you wish the cookie to be valid. You may optionally setthe lifetime of the cookie, and the path for which it should bereturned.</p><p>By default, the lifetime of the cookie is the current browsersession.</p><p>By default, the path for which the cookie will be valid is "/" - thatis, the entire website.</p><p>Several examples are offered here:</p><div class="example"><p><code>RewriteEngine On<br />RewriteRule ^/index.html - [CO=frontdoor:yes:.apache.org:1440:/]</code></p></div><p>This rule doesn't rewrite the request (the "-" rewrite target tellsmod_rewrite to pass the request through unchanged) but sets a cookiecalled 'frontdoor' to a value of 'yes'. The cookie is valid for any hostin the <code>.apache.org</code> domain. It will be set to expire in 1440minutes (24 hours) and will be returned for all URIs.</p><h3><a name="flag_e" id="flag_e">E|env</a></h3><p>With the [E], or [env] flag, you can set the value of an environmentvariable. Note that some environment variables may be set after the ruleis run, thus unsetting what you have set. See <a href="../env.html">theEnvironment Variables document</a> for more details on how Environmentvariables work.</p><p>The following example sets an evironment variable called 'image' to avalue of '1' if the requested URI is an image file. Then, thatenvironment variable is used to exclude those requests from the accesslog.</p><div class="example"><p><code>RewriteRule \.(png|gif|jpg) - [E=image:1]<br />CustomLog logs/access_log combined env=!image</code></p></div><p>Note that this same effect can be obtained using <code class="directive"><a href="../mod/mod_setenvif.html#setenvif">SetEnvIf</a></code>. This technique is offered asan example, not as a recommendation.</p><h3><a name="flag_f" id="flag_f">F|forbidden</a></h3><p>Using the [F] flag causes Apache to return a 403 Forbidden statuscode to the client. While the same behavior can be accomplished usingthe <code class="directive"><a href="../mod/mod_access.html#deny">Deny</a></code> directive, this allows more flexibility in assigning a Forbidden status.</p><p>The following rule will forbid <code>.exe</code> files from beingdownloaded from your server.</p><div class="example"><p><code>RewriteRule \.exe - [F]</code></p></div><p>This example uses the "-" syntax for the rewrite target, which meansthat the requested URI is not modified. There's no reason to rewrite toanother URI, if you're going to forbid the request.</p><h3><a name="flag_g" id="flag_g">G|gone</a></h3><p>The [G] flag forces Apache to return a 410 Gone status with theresponse. This indicates that a resource used to be available, but is nolonger available.</p><p>As with the [F] flag, you will typically use the "-" syntax for therewrite target when using the [G] flag:</p><div class="example"><p><code>RewriteRule oldproduct - [G,NC]</code></p></div><h3><a name="flag_h" id="flag_h">H|handler</a></h3><p>Forces the resulting request to be handled with the specifiedhandler. For example, one might use this to force all files without afile extension to be parsed by the php handler:</p><div class="example"><p><code>RewriteRule !\. - [H=application/x-httpd-php]</code></p></div><p>The regular expression above - <code>!\.</code> - will match any requestthat does not contain the literal <code>.</code> character.</p><h3><a name="flag_l" id="flag_l">L|last</a></h3><p>The [L] flag causes <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> to stop processingthe rule set. In most contexts, this means that if the rule matches, nofurther rules will be processed.</p><p>If you are using <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> in either<code>.htaccess</code> files or in <code class="directive"><a href="../mod/core.html#directory"><Directory></a></code> sections,it is important to have some understanding of how the rules areprocessed. The simplified form of this is that once the rules have beenprocessed, the rewritten request is handed back to the URL parsingengine to do what it may with it. It is possible that as the rewrittenrequest is handled, the <code>.htaccess</code> file or <code class="directive"><a href="../mod/core.html#directory"><Directory></a></code> sectionmay be encountered again, and thus the ruleset may be run again from thestart. Most commonly this will happen if one of the rules causes aredirect - either internal or external - causing the request process tostart over.</p><p>It is therefore important, if you are using <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> directives in one of thesecontext that you take explicit steps to avoid rules looping, and notcount solely on the [L] flag to terminate execution of a series ofrules, as shown below.</p><p>The example given here will rewrite any request to<code>index.php</code>, giving the original request as a query stringargument to <code>index.php</code>, however, if the request is alreadyfor <code>index.php</code>, this rule will be skipped.</p><div class="example"><p><code>RewriteCond %{REQUEST_URI} !index\.php<br />RewriteRule ^(.*) index.php?req=$1 [L]</code></p></div><h3><a name="flag_n" id="flag_n">N|next</a></h3><p>The [N] flag causes the ruleset to start over again from the top. Usewith extreme caution, as it may result in loop.</p><p>The [Next] flag could be used, for example, if you wished to replace acertain string or letter repeatedly in a request. The example shown herewill replace A with B everywhere in a request, and will continue doingso until there are no more As to be replaced.</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -