flags.xml revision f0134a568e11b14c10b8787800faf912010f5d29
6ae232055d4d8a97267517c5e50074c2c819941and<?xml version='1.0' encoding='UTF-8' ?>
6ae232055d4d8a97267517c5e50074c2c819941and<!DOCTYPE manualpage SYSTEM "/style/manualpage.dtd">
6ae232055d4d8a97267517c5e50074c2c819941and<?xml-stylesheet type="text/xsl" href="/style/manual.en.xsl"?>
6ae232055d4d8a97267517c5e50074c2c819941and<!-- $LastChangedRevision$ -->
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<!--
6ae232055d4d8a97267517c5e50074c2c819941and Licensed to the Apache Software Foundation (ASF) under one or more
6ae232055d4d8a97267517c5e50074c2c819941and contributor license agreements. See the NOTICE file distributed with
6ae232055d4d8a97267517c5e50074c2c819941and this work for additional information regarding copyright ownership.
6ae232055d4d8a97267517c5e50074c2c819941and The ASF licenses this file to You under the Apache License, Version 2.0
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen (the "License"); you may not use this file except in compliance with
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen the License. You may obtain a copy of the License at
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen http://www.apache.org/licenses/LICENSE-2.0
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and Unless required by applicable law or agreed to in writing, software
d229f940abfb2490dee17979e9a5ff31b7012eb5rbowen distributed under the License is distributed on an "AS IS" BASIS,
3f08db06526d6901aa08c110b5bc7dde6bc39905nd WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6ae232055d4d8a97267517c5e50074c2c819941and See the License for the specific language governing permissions and
6ae232055d4d8a97267517c5e50074c2c819941and limitations under the License.
6ae232055d4d8a97267517c5e50074c2c819941and-->
b43f840409794ed298e8634f6284741f193b6c4ftakashi
6ae232055d4d8a97267517c5e50074c2c819941and<manualpage metafile="flags.xml.meta">
b43f840409794ed298e8634f6284741f193b6c4ftakashi<parentdocument href="./">Rewrite</parentdocument>
c04f76acce77126cf88b09350e56ea8c6b4a064enilgun
6ae232055d4d8a97267517c5e50074c2c819941and <title>Apache mod_rewrite Flags</title>
91f378b5a10f2d83820902ed10ba7967a3920c18nilgun
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung<summary>
6ae232055d4d8a97267517c5e50074c2c819941and<p>This document discusses the flags which are available to the
b43f840409794ed298e8634f6284741f193b6c4ftakashi<directive module="mod_rewrite">RewriteRule</directive> directive,
b43f840409794ed298e8634f6284741f193b6c4ftakashiproviding detailed explanations and examples. This is not necessarily
b43f840409794ed298e8634f6284741f193b6c4ftakashia comprehensive list of all flags available, so be sure to also
b43f840409794ed298e8634f6284741f193b6c4ftakashiconsult the reference documentation.</p>
6ae232055d4d8a97267517c5e50074c2c819941and</summary>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<seealso><a href="/mod/mod_rewrite.html">Module documentation</a></seealso>
6ae232055d4d8a97267517c5e50074c2c819941and<seealso><a href="tech.html">Technical details</a></seealso>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="introduction"><title>Introduction</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p><directive module="mod_rewrite">RewriteRule</directive>s can have
6ae232055d4d8a97267517c5e50074c2c819941andtheir behavior modified by one or more flags. Flags are included in
6ae232055d4d8a97267517c5e50074c2c819941andsquare brackets at the end of the rule, and multiple flags are separated
6ae232055d4d8a97267517c5e50074c2c819941andby commas.</p>
6ae232055d4d8a97267517c5e50074c2c819941and<example>
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule pattern target [Flag1,Flag2,Flag3]
6ae232055d4d8a97267517c5e50074c2c819941and</example>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>The flags all have a short form, such as <code>CO</code>, as well as
6ae232055d4d8a97267517c5e50074c2c819941anda longer form, such as <code>cookie</code>. Some flags take one or more
6ae232055d4d8a97267517c5e50074c2c819941andarguments. Flags are not case sensitive.</p>
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flags"><title>The flags</title>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>Each flag has a long and short form. While it is most common to use
6ae232055d4d8a97267517c5e50074c2c819941andthe short form, it is recommended that you familiarize yourself with the
6ae232055d4d8a97267517c5e50074c2c819941andlong form, so that you remember what each flag is supposed to do.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>Presented here are each of the available flags, along with an example
6ae232055d4d8a97267517c5e50074c2c819941andof how you might use them.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_b"><title>B</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>The [B] flag instructs <directive
6ae232055d4d8a97267517c5e50074c2c819941andmodule="mod_rewrite">RewriteRule</directive> to escape non-alphanumeric
6ae232055d4d8a97267517c5e50074c2c819941andcharacters before applying the transformation.
6ae232055d4d8a97267517c5e50074c2c819941and</p>
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_c"><title>C|chain</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>The [C] or [chain] flag indicates that the <directive
6ae232055d4d8a97267517c5e50074c2c819941andmodule="mod_rewrite">RewriteRule</directive> is chained to the next
6ae232055d4d8a97267517c5e50074c2c819941andrule. That is, if the rule matches, then it is processed as usual and
6ae232055d4d8a97267517c5e50074c2c819941andcontrol moves on to the next rule. However, if it does not match, then
6ae232055d4d8a97267517c5e50074c2c819941andthe next rule, and any other rules that are chained together, will be
6ae232055d4d8a97267517c5e50074c2c819941andskipped.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_co"><title>CO|cookie</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>The [CO], or [cookie] flag, allows you to set a cookie when a
6ae232055d4d8a97267517c5e50074c2c819941andparticular <directive module="mod_rewrite">RewriteRule</directive>
6ae232055d4d8a97267517c5e50074c2c819941andmatches. The argument consists of three required fields and two optional
6ae232055d4d8a97267517c5e50074c2c819941andfields.</p>
6ae232055d4d8a97267517c5e50074c2c819941and<p>You must declare a name and value for the cookie to be set, and the
6ae232055d4d8a97267517c5e50074c2c819941anddomain for which you wish the cookie to be valid. You may optionally set
6ae232055d4d8a97267517c5e50074c2c819941andthe lifetime of the cookie, and the path for which it should be
6ae232055d4d8a97267517c5e50074c2c819941andreturned.</p>
6ae232055d4d8a97267517c5e50074c2c819941and<p>By default, the lifetime of the cookie is the current browser
6ae232055d4d8a97267517c5e50074c2c819941andsession.</p>
6ae232055d4d8a97267517c5e50074c2c819941and<p>By default, the path for which the cookie will be valid is "/" - that
6ae232055d4d8a97267517c5e50074c2c819941andis, the entire website.</p>
6ae232055d4d8a97267517c5e50074c2c819941and<p>Several examples are offered here:</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<example>
6ae232055d4d8a97267517c5e50074c2c819941andRewriteEngine On<br />
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule ^/index\.html - [CO=frontdoor:yes:.apache.org:1440:/]
6ae232055d4d8a97267517c5e50074c2c819941and</example>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>In the example give, the rule doesn't rewrite the request.
6ae232055d4d8a97267517c5e50074c2c819941andThe "-" rewrite target tells mod_rewrite to pass the request
6ae232055d4d8a97267517c5e50074c2c819941andthrough unchanged. Instead, it sets a cookie
6ae232055d4d8a97267517c5e50074c2c819941andcalled 'frontdoor' to a value of 'yes'. The cookie is valid for any host
6ae232055d4d8a97267517c5e50074c2c819941andin the <code>.apache.org</code> domain. It will be set to expire in 1440
6ae232055d4d8a97267517c5e50074c2c819941andminutes (24 hours) and will be returned for all URIs.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_dpi"><title>DPI|discardpathinfo</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>The DPI flag causes the PATH_INFO portion of the rewritten URI to be
6ae232055d4d8a97267517c5e50074c2c819941anddiscarded.</p>
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_e"><title>E|env</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>With the [E], or [env] flag, you can set the value of an environment
6ae232055d4d8a97267517c5e50074c2c819941andvariable. Note that some environment variables may be set after the rule
6ae232055d4d8a97267517c5e50074c2c819941andis run, thus unsetting what you have set. See <a href="/env.html">the
6ae232055d4d8a97267517c5e50074c2c819941andEnvironment Variables document</a> for more details on how Environment
6ae232055d4d8a97267517c5e50074c2c819941andvariables work.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>The following example sets an evironment variable called 'image' to a
6ae232055d4d8a97267517c5e50074c2c819941andvalue of '1' if the requested URI is an image file. Then, that
6ae232055d4d8a97267517c5e50074c2c819941andenvironment variable is used to exclude those requests from the access
6ae232055d4d8a97267517c5e50074c2c819941andlog.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<example>
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule \.(png|gif|jpg) - [E=image:1]<br />
6ae232055d4d8a97267517c5e50074c2c819941andCustomLog logs/access_log combined env=!image
6ae232055d4d8a97267517c5e50074c2c819941and</example>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>Note that this same effect can be obtained using <directive
6ae232055d4d8a97267517c5e50074c2c819941andmodule="mod_setenvif">SetEnvIf</directive>. This technique is offered as
6ae232055d4d8a97267517c5e50074c2c819941andan example, not as a recommendation.</p>
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_f"><title>F|forbidden</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>Using the [F] flag causes Apache to return a 403 Forbidden status
6ae232055d4d8a97267517c5e50074c2c819941andcode to the client. While the same behavior can be accomplished using
6ae232055d4d8a97267517c5e50074c2c819941andthe <directive module="mod_access">Deny</directive> directive, this
6ae232055d4d8a97267517c5e50074c2c819941andallows more flexibility in assigning a Forbidden status.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>The following rule will forbid <code>.exe</code> files from being
6ae232055d4d8a97267517c5e50074c2c819941anddownloaded from your server.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<example>
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule \.exe - [F]
6ae232055d4d8a97267517c5e50074c2c819941and</example>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>This example uses the "-" syntax for the rewrite target, which means
6ae232055d4d8a97267517c5e50074c2c819941andthat the requested URI is not modified. There's no reason to rewrite to
6ae232055d4d8a97267517c5e50074c2c819941andanother URI, if you're going to forbid the request.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_g"><title>G|gone</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>The [G] flag forces Apache to return a 410 Gone status with the
6ae232055d4d8a97267517c5e50074c2c819941andresponse. This indicates that a resource used to be available, but is no
6ae232055d4d8a97267517c5e50074c2c819941andlonger available.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>As with the [F] flag, you will typically use the "-" syntax for the
6ae232055d4d8a97267517c5e50074c2c819941andrewrite target when using the [G] flag:</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<example>
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule oldproduct - [G,NC]
6ae232055d4d8a97267517c5e50074c2c819941and</example>
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_h"><title>H|handler</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>Forces the resulting request to be handled with the specified
6ae232055d4d8a97267517c5e50074c2c819941andhandler. For example, one might use this to force all files without a
6ae232055d4d8a97267517c5e50074c2c819941andfile extension to be parsed by the php handler:</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<example>
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule !\. - [H=application/x-httpd-php]
6ae232055d4d8a97267517c5e50074c2c819941and</example>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>
6ae232055d4d8a97267517c5e50074c2c819941andThe regular expression above - <code>!\.</code> - will match any request
6ae232055d4d8a97267517c5e50074c2c819941andthat does not contain the literal <code>.</code> character.
6ae232055d4d8a97267517c5e50074c2c819941and</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>This can be also used to force the handler based on some conditions.
6ae232055d4d8a97267517c5e50074c2c819941andFor example, the following snippet used in per-server context allows
6ae232055d4d8a97267517c5e50074c2c819941and<code>.php</code> files to be <em>displayed</em> by <code>mod_php</code>
6ae232055d4d8a97267517c5e50074c2c819941andif they are requested with the <code>.phps</code> extension:</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<example>
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule ^(/source/.+\.php)s$ $1 [H=application/x-httpd-php-source]
6ae232055d4d8a97267517c5e50074c2c819941and</example>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>The regular expression above - <code>^(/source/.+\.php)s$</code> - will
6ae232055d4d8a97267517c5e50074c2c819941andmatch any request that starts with <code>/source/</code> followed by 1 or
6ae232055d4d8a97267517c5e50074c2c819941andn characters followed by <code>.phps</code> literally. The backreference
6ae232055d4d8a97267517c5e50074c2c819941and$1 referrers to the captured match within parenthesis of the regular
6ae232055d4d8a97267517c5e50074c2c819941andexpression.</p>
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_l"><title>L|last</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>The [L] flag causes <module>mod_rewrite</module> to stop processing
6ae232055d4d8a97267517c5e50074c2c819941andthe rule set. In most contexts, this means that if the rule matches, no
6ae232055d4d8a97267517c5e50074c2c819941andfurther rules will be processed.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>If you are using <directive
6ae232055d4d8a97267517c5e50074c2c819941andmodule="mod_rewrite">RewriteRule</directive> in either
6ae232055d4d8a97267517c5e50074c2c819941and<code>.htaccess</code> files or in
6ae232055d4d8a97267517c5e50074c2c819941and<directive type="section" module="core">Directory</directive> sections,
6ae232055d4d8a97267517c5e50074c2c819941andit is important to have some understanding of how the rules are
6ae232055d4d8a97267517c5e50074c2c819941andprocessed. The simplified form of this is that once the rules have been
6ae232055d4d8a97267517c5e50074c2c819941andprocessed, the rewritten request is handed back to the URL parsing
6ae232055d4d8a97267517c5e50074c2c819941andengine to do what it may with it. It is possible that as the rewritten
6ae232055d4d8a97267517c5e50074c2c819941andrequest is handled, the <code>.htaccess</code> file or
6ae232055d4d8a97267517c5e50074c2c819941and<directive type="section" module="core">Directory</directive> section
6ae232055d4d8a97267517c5e50074c2c819941andmay be encountered again, and thus the ruleset may be run again from the
6ae232055d4d8a97267517c5e50074c2c819941andstart. Most commonly this will happen if one of the rules causes a
6ae232055d4d8a97267517c5e50074c2c819941andredirect - either internal or external - causing the request process to
6ae232055d4d8a97267517c5e50074c2c819941andstart over.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>It is therefore important, if you are using <directive
6ae232055d4d8a97267517c5e50074c2c819941andmodule="mod_rewrite">RewriteRule</directive> directives in one of these
6ae232055d4d8a97267517c5e50074c2c819941andcontexts, that you take explicit steps to avoid rules looping, and not
6ae232055d4d8a97267517c5e50074c2c819941andcount solely on the [L] flag to terminate execution of a series of
6ae232055d4d8a97267517c5e50074c2c819941andrules, as shown below.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>The example given here will rewrite any request to
6ae232055d4d8a97267517c5e50074c2c819941and<code>index.php</code>, giving the original request as a query string
6ae232055d4d8a97267517c5e50074c2c819941andargument to <code>index.php</code>, however, the <directive
6ae232055d4d8a97267517c5e50074c2c819941andmodule="mod_rewrite">RewriteCond</directive> ensures that if the request
6ae232055d4d8a97267517c5e50074c2c819941andis already for <code>index.php</code>, the <directive
6ae232055d4d8a97267517c5e50074c2c819941andmodule="mod_rewrite">RewriteRule</directive> will be skipped.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<example>
6ae232055d4d8a97267517c5e50074c2c819941andRewriteCond %{REQUEST_URI} !=index.php<br />
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule ^(.*) index.php?req=$1 [L]
6ae232055d4d8a97267517c5e50074c2c819941and</example>
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_n"><title>N|next</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>
6ae232055d4d8a97267517c5e50074c2c819941andThe [N] flag causes the ruleset to start over again from the top. Use
6ae232055d4d8a97267517c5e50074c2c819941andwith extreme caution, as it may result in loop.
6ae232055d4d8a97267517c5e50074c2c819941and</p>
6ae232055d4d8a97267517c5e50074c2c819941and<p>
6ae232055d4d8a97267517c5e50074c2c819941andThe [Next] flag could be used, for example, if you wished to replace a
6ae232055d4d8a97267517c5e50074c2c819941andcertain string or letter repeatedly in a request. The example shown here
6ae232055d4d8a97267517c5e50074c2c819941andwill replace A with B everywhere in a request, and will continue doing
6ae232055d4d8a97267517c5e50074c2c819941andso until there are no more As to be replaced.
6ae232055d4d8a97267517c5e50074c2c819941and</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<example>
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule (.*)A(.*) $1B$2 [N]
6ae232055d4d8a97267517c5e50074c2c819941and</example>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>You can think of this as a <code>while</code> loop: While this
6ae232055d4d8a97267517c5e50074c2c819941andpattern still matches (i.e., while the URI still contains an
6ae232055d4d8a97267517c5e50074c2c819941and<code>A</code>), perform this substitution (i.e., replace the
6ae232055d4d8a97267517c5e50074c2c819941and<code>A</code> with a <code>B</code>).</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_nc"><title>NC|nocase</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>Use of the [NC] flag causes the <directive
6ae232055d4d8a97267517c5e50074c2c819941andmodule="mod_rewrite">RewriteRule</directive> to be matched in a
6ae232055d4d8a97267517c5e50074c2c819941andcase-insensitive manner. That is, it doesn't care whether letters appear
6ae232055d4d8a97267517c5e50074c2c819941andas upper-case or lower-case in the matched URI.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>In the example below, any request for an image file will be proxied
6ae232055d4d8a97267517c5e50074c2c819941andto your dedicated image server. The match is case-insensitive, so that
6ae232055d4d8a97267517c5e50074c2c819941and<code>.jpg</code> and <code>.JPG</code> files are both acceptable, for
6ae232055d4d8a97267517c5e50074c2c819941andexample.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<example>
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule (.*\.(jpg|gif|png))$ http://images.example.com$1 [P,NC]
6ae232055d4d8a97267517c5e50074c2c819941and</example>
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_ne"><title>NE|noescape</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>By default, special characters, such as <code>&amp;</code> and
6ae232055d4d8a97267517c5e50074c2c819941and<code>?</code>, for example, will be converted to their hexcode
6ae232055d4d8a97267517c5e50074c2c819941andequivalent. Using the [NE] flag prevents that from happening.
6ae232055d4d8a97267517c5e50074c2c819941and</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<example>
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule ^/anchor/(.+) /bigpage.html#$1 [NE,R]
6ae232055d4d8a97267517c5e50074c2c819941and</example>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>
6ae232055d4d8a97267517c5e50074c2c819941andThe above example will redirect <code>/anchor/xyz</code> to
6ae232055d4d8a97267517c5e50074c2c819941and<code>/bigpage.html#xyz</code>. Omitting the [NE] will result in the #
6ae232055d4d8a97267517c5e50074c2c819941andbeing converted to its hexcode equivalent, <code>%23</code>, which will
6ae232055d4d8a97267517c5e50074c2c819941andthen result in a 404 Not Found error condition.
6ae232055d4d8a97267517c5e50074c2c819941and</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_ns"><title>NS|nosubreq</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>Use of the [NS] flag prevents the rule from being used on
6ae232055d4d8a97267517c5e50074c2c819941andsubrequests. For example, a page which is included using an SSI (Server
6ae232055d4d8a97267517c5e50074c2c819941andSide Include) is a subrequest, and you may want to avoid rewrites
6ae232055d4d8a97267517c5e50074c2c819941andhappening on those subrequests.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>
6ae232055d4d8a97267517c5e50074c2c819941andImages, javascript files, or css files, loaded as part of an HTML page,
6ae232055d4d8a97267517c5e50074c2c819941andare not subrequests - the browser requests them as separate HTTP
6ae232055d4d8a97267517c5e50074c2c819941andrequests.
6ae232055d4d8a97267517c5e50074c2c819941and</p>
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_p"><title>P|proxy</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>Use of the [P] flag causes the request to be handled by
6ae232055d4d8a97267517c5e50074c2c819941and<module>mod_proxy</module>, and handled via a proxy request. For
6ae232055d4d8a97267517c5e50074c2c819941andexample, if you wanted all image requests to be handled by a back-end
6ae232055d4d8a97267517c5e50074c2c819941andimage server, you might do something like the following:</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<example>
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule (.*)\.(jpg|gif|png) http://images.example.com$1.$2 [P]
6ae232055d4d8a97267517c5e50074c2c819941and</example>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>Use of the [P] flag implies [L] - that is, the request is immediatly
6ae232055d4d8a97267517c5e50074c2c819941andpushed through the proxy, and any following rules will not be
6ae232055d4d8a97267517c5e50074c2c819941andconsidered.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_pt"><title>PT|passthrough</title>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>
6ae232055d4d8a97267517c5e50074c2c819941andThe target (or substitution string) in a RewriteRule is assumed to be a
6ae232055d4d8a97267517c5e50074c2c819941andfile path, by default. The use of the [PT] flag causes it to be treated
6ae232055d4d8a97267517c5e50074c2c819941andas a URI instead. That is to say, the
6ae232055d4d8a97267517c5e50074c2c819941anduse of the [PT] flag causes the result of the <directive
6ae232055d4d8a97267517c5e50074c2c819941andmodule="mod_rewrite">RewriteRule</directive> to be passed back through
6ae232055d4d8a97267517c5e50074c2c819941andURL mapping, so that location-based mappings, such as <directive
6ae232055d4d8a97267517c5e50074c2c819941andmodule="mod_alias">Alias</directive>, for example, might have a chance to take
6ae232055d4d8a97267517c5e50074c2c819941andeffect.
6ae232055d4d8a97267517c5e50074c2c819941and</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>
6ae232055d4d8a97267517c5e50074c2c819941andIf, for example, you have an
6ae232055d4d8a97267517c5e50074c2c819941and<directive module="mod_alias">Alias</directive>
6ae232055d4d8a97267517c5e50074c2c819941andfor /icons, and have a <directive
6ae232055d4d8a97267517c5e50074c2c819941andmodule="mod_rewrite">RewriteRule</directive> pointing there, you should
6ae232055d4d8a97267517c5e50074c2c819941anduse the [PT] flag to ensure that the
6ae232055d4d8a97267517c5e50074c2c819941and<directive module="mod_alias">Alias</directive> is evaluated.
6ae232055d4d8a97267517c5e50074c2c819941and</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<example>
6ae232055d4d8a97267517c5e50074c2c819941andAlias /icons /usr/local/apache/icons<br />
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule /pics/(.+)\.jpg /icons/$1.gif [PT]
6ae232055d4d8a97267517c5e50074c2c819941and</example>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>
6ae232055d4d8a97267517c5e50074c2c819941andOmission of the [PT] flag in this case will cause the Alias to be
6ae232055d4d8a97267517c5e50074c2c819941andignored, resulting in a 'File not found' error being returned.
6ae232055d4d8a97267517c5e50074c2c819941and</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_qsa"><title>QSA|qsappend</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>
6ae232055d4d8a97267517c5e50074c2c819941andWhen the replacement URI contains a query string, the default behavior
6ae232055d4d8a97267517c5e50074c2c819941andof <directive module="mod_rewrite">RewriteRule</directive> is to discard
6ae232055d4d8a97267517c5e50074c2c819941andthe existing query string, and replace it with the newly generated one.
6ae232055d4d8a97267517c5e50074c2c819941andUsing the [QSA] flag causes the query strings to be combined.
6ae232055d4d8a97267517c5e50074c2c819941and</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>Consider the following rule:</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<example>
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule /pages/(.+) /page.php?page=$1 [QSA]
6ae232055d4d8a97267517c5e50074c2c819941and</example>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>With the [QSA] flag, a request for <code>/pages/123?one=two</code> will be
6ae232055d4d8a97267517c5e50074c2c819941andmapped to <code>/page.php?page=123&amp;one=two</code>. Without the [QSA]
6ae232055d4d8a97267517c5e50074c2c819941andflag, that same request will be mapped to
6ae232055d4d8a97267517c5e50074c2c819941and<code>/page.php?page=123</code> - that is, the existing query string
6ae232055d4d8a97267517c5e50074c2c819941andwill be discarded.
6ae232055d4d8a97267517c5e50074c2c819941and</p>
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_qsd"><title>QSD|qsdiscard</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>
6ae232055d4d8a97267517c5e50074c2c819941andWhen the requested URI contains a query string, and the target URI does
6ae232055d4d8a97267517c5e50074c2c819941andnot, the default behavior of <directive
6ae232055d4d8a97267517c5e50074c2c819941andmodule="mod_rewrite">RewriteRule</directive> is to copy that query
6ae232055d4d8a97267517c5e50074c2c819941andstring to the target URI. Using the [QSD] flag causes the query string
6ae232055d4d8a97267517c5e50074c2c819941andto be discarded.
6ae232055d4d8a97267517c5e50074c2c819941and</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>
6ae232055d4d8a97267517c5e50074c2c819941andUsing [QSD] and [QSA] together will result in [QSD] taking preference.
6ae232055d4d8a97267517c5e50074c2c819941and</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>
6ae232055d4d8a97267517c5e50074c2c819941andIf the target URI has a query string, the default behavior will be
6ae232055d4d8a97267517c5e50074c2c819941andobserved - that is, the original query string will be discarded and
6ae232055d4d8a97267517c5e50074c2c819941andreplaced with the query string in the <code>RewriteRule</code> target
6ae232055d4d8a97267517c5e50074c2c819941andURI.
6ae232055d4d8a97267517c5e50074c2c819941and</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_r"><title>R|redirect</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>
6ae232055d4d8a97267517c5e50074c2c819941andUse of the [R] flag causes a HTTP redirect to be issued to the browser.
6ae232055d4d8a97267517c5e50074c2c819941andIf a fully-qualified URL is specified (that is, including
6ae232055d4d8a97267517c5e50074c2c819941and<code>http://servername/</code>) then a redirect will be issued to that
6ae232055d4d8a97267517c5e50074c2c819941andlocation. Otherwise, the current servername will be used to generate the
6ae232055d4d8a97267517c5e50074c2c819941andURL sent with the redirect.
6ae232055d4d8a97267517c5e50074c2c819941and</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>
6ae232055d4d8a97267517c5e50074c2c819941and<em>Any</em> status code may be specified, that is a valid HTTP Response,
6ae232055d4d8a97267517c5e50074c2c819941andwith a 302 status code being used by default if none is specified.
6ae232055d4d8a97267517c5e50074c2c819941and</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>
6ae232055d4d8a97267517c5e50074c2c819941andYou will almost always want to use [R] in conjunction with [L] (that is,
6ae232055d4d8a97267517c5e50074c2c819941anduse [R,L]) because on its own, the [R] flag prepends
6ae232055d4d8a97267517c5e50074c2c819941and<code>http://thishost[:thisport]</code> to the URI, but then passes this
6ae232055d4d8a97267517c5e50074c2c819941andon to the next rule in the ruleset, which can often result in 'Invalid
6ae232055d4d8a97267517c5e50074c2c819941andURI in request' warnings.
6ae232055d4d8a97267517c5e50074c2c819941and</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_s"><title>S|skip</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>The [S] flag is used to skip rules that you don't want to run. This
6ae232055d4d8a97267517c5e50074c2c819941andcan be thought of as a <code>goto</code> statement in your rewrite
6ae232055d4d8a97267517c5e50074c2c819941andruleset. In the following example, we only want to run the <directive
6ae232055d4d8a97267517c5e50074c2c819941andmodule="mod_rewrite">RewriteRule</directive> if the requested URI
6ae232055d4d8a97267517c5e50074c2c819941anddoesn't correspond with an actual file.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<example>
6ae232055d4d8a97267517c5e50074c2c819941and# Is the request for a non-existent file?<br />
6ae232055d4d8a97267517c5e50074c2c819941andRewriteCond %{REQUEST_FILENAME} !-f<br />
6ae232055d4d8a97267517c5e50074c2c819941andRewriteCond %{REQUEST_FILENAME} !-d<br />
6ae232055d4d8a97267517c5e50074c2c819941and# If so, skip these two RewriteRules<br />
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule .? - [S=2]<br />
6ae232055d4d8a97267517c5e50074c2c819941and<br />
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule (.*\.gif) images.php?$1<br />
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule (.*\.html) docs.php?$1
6ae232055d4d8a97267517c5e50074c2c819941and</example>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>This technique is useful because a <directive
6ae232055d4d8a97267517c5e50074c2c819941andmodule="mod_rewrite">RewriteCond</directive> only applies to the
6ae232055d4d8a97267517c5e50074c2c819941and<directive module="mod_rewrite">RewriteRule</directive> immediately
6ae232055d4d8a97267517c5e50074c2c819941andfollowing it. Thus, if you want to make a <code>RewriteCond</code> apply
6ae232055d4d8a97267517c5e50074c2c819941andto several <code>RewriteRule</code>s, one possible technique is to
6ae232055d4d8a97267517c5e50074c2c819941andnegate those conditions and use a [Skip] flag.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<section id="flag_t"><title>T|type</title>
6ae232055d4d8a97267517c5e50074c2c819941and<p>Sets the MIME type with which the resulting response will be
6ae232055d4d8a97267517c5e50074c2c819941andsent. This has the same effect as the <directive
6ae232055d4d8a97267517c5e50074c2c819941andmodule="mod_mime">AddType</directive> directive.</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>For example, you might use the following technique to serve Perl
6ae232055d4d8a97267517c5e50074c2c819941andsource code as plain text, if requested in a particular way:</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<example>
6ae232055d4d8a97267517c5e50074c2c819941and# Serve .pl files as plain text<br />
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule \.pl$ - [T=text/plain]
6ae232055d4d8a97267517c5e50074c2c819941and</example>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>Or, perhaps, if you have a camera that produces jpeg images without
6ae232055d4d8a97267517c5e50074c2c819941andfile extensions, you could force those images to be served with the
6ae232055d4d8a97267517c5e50074c2c819941andcorrect MIME type by virtue of their file names:</p>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<example>
6ae232055d4d8a97267517c5e50074c2c819941and# Files with 'IMG' in the name are jpg images.<br />
6ae232055d4d8a97267517c5e50074c2c819941andRewriteRule IMG - [T=image/jpg]
6ae232055d4d8a97267517c5e50074c2c819941and</example>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and<p>Please note that this is a trivial example, and could be better done
6ae232055d4d8a97267517c5e50074c2c819941andusing <directive type="section" module="core">FilesMatch</directive>
6ae232055d4d8a97267517c5e50074c2c819941andinstead. Always consider the alternate
6ae232055d4d8a97267517c5e50074c2c819941andsolutions to a problem before resorting to rewrite, which will
6ae232055d4d8a97267517c5e50074c2c819941andinvariably be a less efficient solution than the alternatives.</p>
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and</section>
6ae232055d4d8a97267517c5e50074c2c819941and</manualpage>
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and