1 <?xml version='1.0' encoding='UTF-8' ?>
2 <!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <manualpage metafile="flags.xml.meta">
24 <parentdocument href="./">Rewrite</parentdocument>
26 <title>Apache mod_rewrite Flags</title>
29 <p>This document discusses the flags which are available to the
30 <directive module="mod_rewrite">RewriteRule</directive> directive,
31 providing detailed explanations and examples. This is not necessarily
32 a comprehensive list of all flags available, so be sure to also
33 consult the reference documentation.</p>
36 <seealso><a href="../mod/mod_rewrite.html">Module documentation</a></seealso>
37 <seealso><a href="tech.html">Technical details</a></seealso>
38 <seealso><a href="rewrite_guide.html">Rewrite Guide - useful examples</a></seealso>
39 <seealso><a href="rewrite_guide_advanced.html">Advanced Rewrite Guide -
40 advanced useful examples</a></seealso>
42 <section id="introduction"><title>Introduction</title>
43 <p><directive module="mod_rewrite">RewriteRule</directive>s can have
44 their behavior modified by one or more flags. Flags are included in
45 square brackets at the end of the rule, and multiple flags are separated
48 RewriteRule pattern target [Flag1,Flag2,Flag3]
51 <p>The flags all have a short form, such as <code>CO</code>, as well as
52 a longer form, such as <code>cookie</code>. Some flags take one or more
53 arguments. Flags are not case sensitive.</p>
57 <section id="flags"><title>The flags</title>
59 <p>Each flag has a long and short form. While it is most common to use
60 the short form, it is recommended that you familiarize yourself with the
61 long form, so that you remember what each flag is supposed to do.</p>
63 <p>Presented here are each of the available flags, along with an example
64 of how you might use them.</p>
66 <section id="flag_b"><title>B</title>
67 <p>The [B] flag instructs <directive
68 module-"mod_rewrite">RewriteRule</a> to escape non-alphanumeric
69 characters before applying the transformation.
73 <section id="flag_c"><title>C|chain</title>
74 <p>The [C] or [chain] flag indicates that the <directive
75 module="mod_rewrite">RewriteRule</directive> is chained to the next
76 rule. That is, if the rule matches, then it is processed as usual and
77 control moves on to the next rule. However, if it does not match, then
78 the next rule, and any other rules that are chained together, will be
83 <section id="flag_co"><title>CO|cookie</title>
84 <p>The [CO], or [cookie] flag, allows you to set a cookie when a
85 particular <directive module="mod_rewrite">RewriteRule</directive>
86 matches. The argument consists of three required fields and two optional
88 <p>You must declare a name and value for the cookie to be set, and the
89 domain for which you wish the cookie to be valid. You may optionally set
90 the lifetime of the cookie, and the path for which it should be
92 <p>By default, the lifetime of the cookie is the current browser
94 <p>By default, the path for which the cookie will be valid is "/" - that
95 is, the entire website.</p>
96 <p>Several examples are offered here:</p>
99 RewriteEngine On<br />
100 RewriteRule ^/index\.html - [CO=frontdoor:yes:.apache.org:1440:/]
103 <p>In the example give, the rule doesn't rewrite the request.
104 The "-" rewrite target tells mod_rewrite to pass the request
105 through unchanged. Instead, it sets a cookie
106 called 'frontdoor' to a value of 'yes'. The cookie is valid for any host
107 in the <code>.apache.org</code> domain. It will be set to expire in 1440
108 minutes (24 hours) and will be returned for all URIs.</p>
112 <section id="flag_dpi"><title>DPI|discardpathinfo</title>
113 <p>The DPI flag causes the PATH_INFO portion of the rewritten URI to be
117 <section id="flag_e"><title>E|env</title>
118 <p>With the [E], or [env] flag, you can set the value of an environment
119 variable. Note that some environment variables may be set after the rule
120 is run, thus unsetting what you have set. See <a href="../env.html">the
121 Environment Variables document</a> for more details on how Environment
124 <p>The following example sets an evironment variable called 'image' to a
125 value of '1' if the requested URI is an image file. Then, that
126 environment variable is used to exclude those requests from the access
130 RewriteRule \.(png|gif|jpg) - [E=image:1]<br />
131 CustomLog logs/access_log combined env=!image
134 <p>Note that this same effect can be obtained using <directive
135 module="mod_setenvif">SetEnvIf</directive>. This technique is offered as
136 an example, not as a recommendation.</p>
139 <section id="flag_f"><title>F|forbidden</title>
140 <p>Using the [F] flag causes Apache to return a 403 Forbidden status
141 code to the client. While the same behavior can be accomplished using
142 the <directive module="mod_access">Deny</directive> directive, this
143 allows more flexibility in assigning a Forbidden status.</p>
145 <p>The following rule will forbid <code>.exe</code> files from being
146 downloaded from your server.</p>
149 RewriteRule \.exe - [F]
152 <p>This example uses the "-" syntax for the rewrite target, which means
153 that the requested URI is not modified. There's no reason to rewrite to
154 another URI, if you're going to forbid the request.</p>
158 <section id="flag_g"><title>G|gone</title>
159 <p>The [G] flag forces Apache to return a 410 Gone status with the
160 response. This indicates that a resource used to be available, but is no
161 longer available.</p>
163 <p>As with the [F] flag, you will typically use the "-" syntax for the
164 rewrite target when using the [G] flag:</p>
167 RewriteRule oldproduct - [G,NC]
171 <section id="flag_h"><title>H|handler</title>
172 <p>Forces the resulting request to be handled with the specified
173 handler. For example, one might use this to force all files without a
174 file extension to be parsed by the php handler:</p>
177 RewriteRule !\. - [H=application/x-httpd-php]
181 The regular expression above - <code>!\.</code> - will match any request
182 that does not contain the literal <code>.</code> character.
185 <p>This can be also used to force the handler based on some conditions.
186 For example, the following snippet used in per-server context allows
187 <code>.php</code> files to be <em>displayed</em> by <code>mod_php</code>
188 if they are requested with the <code>.phps</code> extension:</p>
191 RewriteRule ^(/source/.+\.php)s$ $1 [H=application/x-httpd-php-source]
194 <p>The regular expression above - <code>^(/source/.+\.php)s$</code> - will
195 match any request that starts with <code>/source/</code> followed by 1 or
196 n characters followed by <code>.phps</code> literally. The backreference
197 $1 referrers to the captured match within parenthesis of the regular
201 <section id="flag_l"><title>L|last</title>
202 <p>The [L] flag causes <module>mod_rewrite</module> to stop processing
203 the rule set. In most contexts, this means that if the rule matches, no
204 further rules will be processed.</p>
206 <p>If you are using <directive
207 module="mod_rewrite">RewriteRule</directive> in either
208 <code>.htaccess</code> files or in
209 <directive type="section" module="core">Directory</directive> sections,
210 it is important to have some understanding of how the rules are
211 processed. The simplified form of this is that once the rules have been
212 processed, the rewritten request is handed back to the URL parsing
213 engine to do what it may with it. It is possible that as the rewritten
214 request is handled, the <code>.htaccess</code> file or
215 <directive type="section" module="core">Directory</directive> section
216 may be encountered again, and thus the ruleset may be run again from the
217 start. Most commonly this will happen if one of the rules causes a
218 redirect - either internal or external - causing the request process to
221 <p>It is therefore important, if you are using <directive
222 module="mod_rewrite">RewriteRule</directive> directives in one of these
223 contexts, that you take explicit steps to avoid rules looping, and not
224 count solely on the [L] flag to terminate execution of a series of
225 rules, as shown below.</p>
227 <p>The example given here will rewrite any request to
228 <code>index.php</code>, giving the original request as a query string
229 argument to <code>index.php</code>, however, the <directive
230 module="mod_rewrite">RewriteCond</directive> ensures that if the request
231 is already for <code>index.php</code>, the <directive
232 module="mod_rewrite">RewriteRule</directive> will be skipped.</p>
235 RewriteCond %{REQUEST_URI} !=index.php<br />
236 RewriteRule ^(.*) index.php?req=$1 [L]
240 <section id="flag_n"><title>N|next</title>
242 The [N] flag causes the ruleset to start over again from the top. Use
243 with extreme caution, as it may result in loop.
246 The [Next] flag could be used, for example, if you wished to replace a
247 certain string or letter repeatedly in a request. The example shown here
248 will replace A with B everywhere in a request, and will continue doing
249 so until there are no more As to be replaced.
253 RewriteRule (.*)A(.*) $1B$2 [N]
256 <p>You can think of this as a <code>while</code> loop: While this
257 pattern still matches (i.e., while the URI still contains an
258 <code>A</code>), perform this substitution (i.e., replace the
259 <code>A</code> with a <code>B</code>).</p>
263 <section id="flag_nc"><title>NC|nocase</title>
264 <p>Use of the [NC] flag causes the <directive
265 module="mod_rewrite">RewriteRule</directive> to be matched in a
266 case-insensitive manner. That is, it doesn't care whether letters appear
267 as upper-case or lower-case in the matched URI.</p>
269 <p>In the example below, any request for an image file will be proxied
270 to your dedicated image server. The match is case-insensitive, so that
271 <code>.jpg</code> and <code>.JPG</code> files are both acceptable, for
275 RewriteRule (.*\.(jpg|gif|png))$ http://images.example.com$1 [P,NC]
279 <section id="flag_ne"><title>NE|noescape</title>
280 <p>By default, special characters, such as <code>&</code> and
281 <code>?</code>, for example, will be converted to their hexcode
282 equivalent. Using the [NE] flag prevents that from happening.
286 RewriteRule ^/anchor/(.+) /bigpage.html#$1 [NE,R]
290 The above example will redirect <code>/anchor/xyz</code> to
291 <code>/bigpage.html#xyz</code>. Omitting the [NE] will result in the #
292 being converted to its hexcode equivalent, <code>%23</code>, which will
293 then result in a 404 Not Found error condition.
298 <section id="flag_ns"><title>NS|nosubreq</title>
299 <p>Use of the [NS] flag prevents the rule from being used on
300 subrequests. For example, a page which is included using an SSI (Server
301 Side Include) is a subrequest, and you may want to avoid rewrites
302 happening on those subrequests.</p>
305 Images, javascript files, or css files, loaded as part of an HTML page,
306 are not subrequests - the browser requests them as separate HTTP
311 <section id="flag_p"><title>P|proxy</title>
312 <p>Use of the [P] flag causes the request to be handled by
313 <module>mod_proxy</module>, and handled via a proxy request. For
314 example, if you wanted all image requests to be handled by a back-end
315 image server, you might do something like the following:</p>
318 RewriteRule (.*)\.(jpg|gif|png) http://images.example.com$1.$2 [P]
321 <p>Use of the [P] flag implies [L] - that is, the request is immediatly
322 pushed through the proxy, and any following rules will not be
327 <section id="flag_pt"><title>PT|passthrough</title>
330 The target (or substitution string) in a RewriteRule is assumed to be a
331 file path, by default. The use of the [PT] flag causes it to be treated
332 as a URI instead. That is to say, the
333 use of the [PT] flag causes the result of the <directive
334 module="mod_rewrite">RewriteRule</directive> to be passed back through
335 URL mapping, so that location-based mappings, such as <directive
336 module="mod_alias">Alias</directive>, for example, might have a chance to take
341 If, for example, you have an
342 <directive module="mod_alias">Alias</directive>
343 for /icons, and have a <directive
344 module="mod_rewrite">RewriteRule</directive> pointing there, you should
345 use the [PT] flag to ensure that the
346 <directive module="mod_alias">Alias</directive> is evaluated.
350 Alias /icons /usr/local/apache/icons<br />
351 RewriteRule /pics/(.+)\.jpg /icons/$1.gif [PT]
355 Omission of the [PT] flag in this case will cause the Alias to be
356 ignored, resulting in a 'File not found' error being returned.
361 <section id="flag_qsa"><title>QSA|qsappend</title>
363 When the replacement URI contains a query string, the default behavior
364 of <directive module="mod_rewrite">RewriteRule</directive> is to discard
365 the existing query string, and replace it with the newly generated one.
366 Using the [QSA] flag causes the query strings to be combined.
369 <p>Consider the following rule:</p>
372 RewriteRule /pages/(.+) /page.php?page=$1 [QSA]
375 <p>With the [QSA] flag, a request for <code>/pages/123?one=two</code> will be
376 mapped to <code>/page.php?page=123&one=two</code>. Without the [QSA]
377 flag, that same request will be mapped to
378 <code>/page.php?page=123</code> - that is, the existing query string
383 <section id="flag_r"><title>R|redirect</title>
385 Use of the [R] flag causes a HTTP redirect to be issued to the browser.
386 If a fully-qualified URL is specified (that is, including
387 <code>http://servername/</code>) then a redirect will be issued to that
388 location. Otherwise, the current servername will be used to generate the
389 URL sent with the redirect.
393 A status code may be specified, in the range 300-399, with a 302 status
394 code being used by default if none is specified.
398 You will almost always want to use [R] in conjunction with [L] (that is,
399 use [R,L]) because on its own, the [R] flag prepends
400 <code>http://thishost[:thisport]</code> to the URI, but then passes this
401 on to the next rule in the ruleset, which can often result in 'Invalid
402 URI in request' warnings.
407 <section id="flag_s"><title>S|skip</title>
408 <p>The [S] flag is used to skip rules that you don't want to run. This
409 can be thought of as a <code>goto</code> statement in your rewrite
410 ruleset. In the following example, we only want to run the <directive
411 module="mod_rewrite">RewriteRule</directive> if the requested URI
412 doesn't correspond with an actual file.</p>
415 # Is the request for a non-existent file?<br />
416 RewriteCond %{REQUEST_FILENAME} !-f<br />
417 RewriteCond %{REQUEST_FILENAME} !-d<br />
418 # If so, skip these two RewriteRules<br />
419 RewriteRule .? - [S=2]<br />
421 RewriteRule (.*\.gif) images.php?$1<br />
422 RewriteRule (.*\.html) docs.php?$1
425 <p>This technique is useful because a <directive
426 module="mod_rewrite">RewriteCond</directive> only applies to the
427 <directive module="mod_rewrite">RewriteRule</directive> immediately
428 following it. Thus, if you want to make a <code>RewriteCond</code> apply
429 to several <code>RewriteRule</code>s, one possible technique is to
430 negate those conditions and use a [Skip] flag.</p>
434 <section id="flag_t"><title>T|type</title>
435 <p>Sets the MIME type with which the resulting response will be
436 sent. This has the same effect as the <directive
437 module="mod_mime">AddType</directive> directive.</p>
439 <p>For example, you might use the following technique to serve Perl
440 source code as plain text, if requested in a particular way:</p>
443 # Serve .pl files as plain text<br />
444 RewriteRule \.pl$ - [T=text/plain]
447 <p>Or, perhaps, if you have a camera that produces jpeg images without
448 file extensions, you could force those images to be served with the
449 correct MIME type by virtue of their file names:</p>
452 # Files with 'IMG' in the name are jpg images.<br />
453 RewriteRule IMG - [T=image/jpg]
456 <p>Please note that this is a trivial example, and could be better done
457 using <directive type="section" module="core">FilesMatch</directive>
458 instead. Always consider the alternate
459 solutions to a problem before resorting to rewrite, which will
460 invariably be a less efficient solution than the alternatives.</p>