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="rewrite_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_c"><title>C|chain</title>
67 <p>The [C] or [chain] flag indicates that the <directive
68 module="mod_rewrite">RewriteRule</directive> is chained to the next
69 rule. That is, if the rule matches, then it is processed as usual and
70 control moves on to the next rule. However, if it does not match, then
71 the next rule, and any other rules that are chained together, will be
76 <section id="flag_co"><title>CO|cookie</title>
77 <p>The [CO], or [cookie] flag, allows you to set a cookie when a
78 particular <directive module="mod_rewrite">RewriteRule</directive>
79 matches. The argument consists of three required fields and two optional
81 <p>You must declare a name and value for the cookie to be set, and the
82 domain for which you wish the cookie to be valid. You may optionally set
83 the lifetime of the cookie, and the path for which it should be
85 <p>By default, the lifetime of the cookie is the current browser
87 <p>By default, the path for which the cookie will be valid is "/" - that
88 is, the entire website.</p>
89 <p>Several examples are offered here:</p>
92 RewriteEngine On<br />
93 RewriteRule ^/index\.html - [CO=frontdoor:yes:.apache.org:1440:/]
96 <p>This rule doesn't rewrite the request (the "-" rewrite target tells
97 mod_rewrite to pass the request through unchanged) but sets a cookie
98 called 'frontdoor' to a value of 'yes'. The cookie is valid for any host
99 in the <code>.apache.org</code> domain. It will be set to expire in 1440
100 minutes (24 hours) and will be returned for all URIs.</p>
104 <section id="flag_e"><title>E|env</title>
105 <p>With the [E], or [env] flag, you can set the value of an environment
106 variable. Note that some environment variables may be set after the rule
107 is run, thus unsetting what you have set. See <a href="../env.html">the
108 Environment Variables document</a> for more details on how Environment
111 <p>The following example sets an evironment variable called 'image' to a
112 value of '1' if the requested URI is an image file. Then, that
113 environment variable is used to exclude those requests from the access
117 RewriteRule \.(png|gif|jpg) - [E=image:1]<br />
118 CustomLog logs/access_log combined env=!image
121 <p>Note that this same effect can be obtained using <directive
122 module="mod_setenvif">SetEnvIf</directive>. This technique is offered as
123 an example, not as a recommendation.</p>
126 <section id="flag_f"><title>F|forbidden</title>
127 <p>Using the [F] flag causes Apache to return a 403 Forbidden status
128 code to the client. While the same behavior can be accomplished using
129 the <directive module="mod_access">Deny</directive> directive, this
130 allows more flexibility in assigning a Forbidden status.</p>
132 <p>The following rule will forbid <code>.exe</code> files from being
133 downloaded from your server.</p>
136 RewriteRule \.exe - [F]
139 <p>This example uses the "-" syntax for the rewrite target, which means
140 that the requested URI is not modified. There's no reason to rewrite to
141 another URI, if you're going to forbid the request.</p>
145 <section id="flag_g"><title>G|gone</title>
146 <p>The [G] flag forces Apache to return a 410 Gone status with the
147 response. This indicates that a resource used to be available, but is no
148 longer available.</p>
150 <p>As with the [F] flag, you will typically use the "-" syntax for the
151 rewrite target when using the [G] flag:</p>
154 RewriteRule oldproduct - [G,NC]
158 <section id="flag_h"><title>H|handler</title>
159 <p>Forces the resulting request to be handled with the specified
160 handler. For example, one might use this to force all files without a
161 file extension to be parsed by the php handler:</p>
164 RewriteRule !\. - [H=application/x-httpd-php]
168 The regular expression above - <code>!\.</code> - will match any request
169 that does not contain the literal <code>.</code> character.
172 <p>This can be also used to force the handler based on some conditions.
173 For example, the following snippet used in per-server context allows
174 <code>.php</code> files to be <em>displayed</em> by <code>mod_php</code>
175 if they are requested with the <code>.phps</code> extension:</p>
178 RewriteRule ^(/source/.+\.php)s$ $1 [H=application/x-httpd-php-source]
181 <p>The regular expression above - <code>^(/source/.+\.php)s$</code> - will
182 match any request that starts with <code>/source/</code> followed by 1 or
183 n characters followed by <code>.phps</code> literally. The backreference
184 $1 referrers to the captured match within parenthesis of the regular
188 <section id="flag_l"><title>L|last</title>
189 <p>The [L] flag causes <module>mod_rewrite</module> to stop processing
190 the rule set. In most contexts, this means that if the rule matches, no
191 further rules will be processed.</p>
193 <p>If you are using <directive
194 module="mod_rewrite">RewriteRule</directive> in either
195 <code>.htaccess</code> files or in
196 <directive type="section" module="core">Directory</directive> sections,
197 it is important to have some understanding of how the rules are
198 processed. The simplified form of this is that once the rules have been
199 processed, the rewritten request is handed back to the URL parsing
200 engine to do what it may with it. It is possible that as the rewritten
201 request is handled, the <code>.htaccess</code> file or
202 <directive type="section" module="core">Directory</directive> section
203 may be encountered again, and thus the ruleset may be run again from the
204 start. Most commonly this will happen if one of the rules causes a
205 redirect - either internal or external - causing the request process to
208 <p>It is therefore important, if you are using <directive
209 module="mod_rewrite">RewriteRule</directive> directives in one of these
210 contexts, that you take explicit steps to avoid rules looping, and not
211 count solely on the [L] flag to terminate execution of a series of
212 rules, as shown below.</p>
214 <p>The example given here will rewrite any request to
215 <code>index.php</code>, giving the original request as a query string
216 argument to <code>index.php</code>, however, the <directive
217 module="mod_rewrite">RewriteCond</directive> ensures that if the request
218 is already for <code>index.php</code>, the <directive
219 module="mod_rewrite">RewriteRule</directive> will be skipped.</p>
222 RewriteCond %{REQUEST_URI} !=index.php<br />
223 RewriteRule ^(.*) index.php?req=$1 [L]
227 <section id="flag_n"><title>N|next</title>
229 The [N] flag causes the ruleset to start over again from the top. Use
230 with extreme caution, as it may result in loop.
233 The [Next] flag could be used, for example, if you wished to replace a
234 certain string or letter repeatedly in a request. The example shown here
235 will replace A with B everywhere in a request, and will continue doing
236 so until there are no more As to be replaced.
240 RewriteRule (.*)A(.*) $1B$2 [N]
243 <p>You can think of this as a <code>while</code> loop: While this
244 pattern still matches (i.e., while the URI still contains an
245 <code>A</code>), perform this substitution (i.e., replace the
246 <code>A</code> with a <code>B</code>).</p>
250 <section id="flag_nc"><title>NC|nocase</title>
251 <p>Use of the [NC] flag causes the <directive
252 module="mod_rewrite">RewriteRule</directive> to be matched in a
253 case-insensitive manner. That is, it doesn't care whether letters appear
254 as upper-case or lower-case in the matched URI.</p>
256 <p>In the example below, any request for an image file will be proxied
257 to your dedicated image server. The match is case-insensitive, so that
258 <code>.jpg</code> and <code>.JPG</code> files are both acceptable, for
262 RewriteRule (.*\.(jpg|gif|png))$ http://images.example.com$1 [P,NC]
266 <section id="flag_ne"><title>NE|noescape</title>
267 <p>By default, special characters, such as <code>&</code> and
268 <code>?</code>, for example, will be converted to their hexcode
269 equivalent. Using the [NE] flag prevents that from happening.
273 RewriteRule ^/anchor/(.+) /bigpage.html#$1 [NE,R]
277 The above example will redirect <code>/anchor/xyz</code> to
278 <code>/bigpage.html#xyz</code>. Omitting the [NE] will result in the #
279 being converted to its hexcode equivalent, <code>%23</code>, which will
280 then result in a 404 Not Found error condition.
285 <section id="flag_ns"><title>NS|nosubreq</title>
286 <p>Use of the [NS] flag prevents the rule from being used on
287 subrequests. For example, a page which is included using an SSI (Server
288 Side Include) is a subrequest, and you may want to avoid rewrites
289 happening on those subrequests.</p>
292 Images, javascript files, or css files, loaded as part of an HTML page,
293 are not subrequests - the browser requests them as separate HTTP
298 <section id="flag_p"><title>P|proxy</title>
299 <p>Use of the [P] flag causes the request to be handled by
300 <module>mod_proxy</module>, and handled via a proxy request. For
301 example, if you wanted all image requests to be handled by a back-end
302 image server, you might do something like the following:</p>
305 RewriteRule (.*)\.(jpg|gif|png) http://images.example.com$1.$2 [P]
308 <p>Use of the [P] flag implies [L] - that is, the request is immediatly
309 pushed through the proxy, and any following rules will not be
314 <section id="flag_pt"><title>PT|passthrough</title>
317 The target (or substitution string) in a RewriteRule is assumed to be a
318 file path, by default. The use of the [PT] flag causes it to be treated
319 as a URI instead. That is to say, the
320 use of the [PT] flag causes the result of the <directive
321 module="mod_rewrite">RewriteRule</directive> to be passed back through
322 URL mapping, so that location-based mappings, such as <directive
323 module="mod_alias">Alias</directive>, for example, might have a chance to take
328 If, for example, you have an
329 <directive module="mod_alias">Alias</directive>
330 for /icons, and have a <directive
331 module="mod_rewrite">RewriteRule</directive> pointing there, you should
332 use the [PT] flag to ensure that the
333 <directive module="mod_alias">Alias</directive> is evaluated.
337 Alias /icons /usr/local/apache/icons<br />
338 RewriteRule /pics/(.+)\.jpg /icons/$1.gif [PT]
342 Omission of the [PT] flag in this case will cause the Alias to be
343 ignored, resulting in a 'File not found' error being returned.
348 <section id="flag_qsa"><title>QSA|qsappend</title>
350 When the replacement URI contains a query string, the default behavior
351 of <directive module="mod_rewrite">RewriteRule</directive> is to discard
352 the existing query string, and replace it with the newly generated one.
353 Using the [QSA] flag causes the query strings to be combined.
356 <p>Consider the following rule:</p>
359 RewriteRule /pages/(.+) /page.php?page=$1 [QSA]
362 <p>With the [QSA] flag, a request for <code>/pages/123?one=two</code> will be
363 mapped to <code>/page.php?page=123&one=two</code>. Without the [QSA]
364 flag, that same request will be mapped to
365 <code>/page.php?page=123</code> - that is, the existing query string
370 <section id="flag_r"><title>R|redirect</title>
372 Use of the [R] flag causes a HTTP redirect to be issued to the browser.
373 If a fully-qualified URL is specified (that is, including
374 <code>http://servername/</code>) then a redirect will be issued to that
375 location. Otherwise, the current servername will be used to generate the
376 URL sent with the redirect.
380 A status code may be specified, in the range 300-399, with a 302 status
381 code being used by default if none is specified.
385 You will almost always want to use [R] in conjunction with [L] (that is,
386 use [R,L]) because on its own, the [R] flag prepends
387 <code>http://thishost[:thisport]</code> to the URI, but then passes this
388 on to the next rule in the ruleset, which can often result in 'Invalid
389 URI in request' warnings.
394 <section id="flag_s"><title>S|skip</title>
395 <p>The [S] flag is used to skip rules that you don't want to run. This
396 can be thought of as a <code>goto</code> statement in your rewrite
397 ruleset. In the following example, we only want to run the <directive
398 module="mod_rewrite">RewriteRule</directive> if the requested URI
399 doesn't correspond with an actual file.</p>
402 # Is the request for a non-existent file?<br />
403 RewriteCond %{REQUEST_FILENAME} !-f<br />
404 RewriteCond %{REQUEST_FILENAME} !-d<br />
405 # If so, skip these two RewriteRules<br />
406 RewriteRule .? - [S=2]<br />
408 RewriteRule (.*\.gif) images.php?$1<br />
409 RewriteRule (.*\.html) docs.php?$1
412 <p>This technique is useful because a <directive
413 module="mod_rewrite">RewriteCond</directive> only applies to the
414 <directive module="mod_rewrite">RewriteRule</directive> immediately
415 following it. Thus, if you want to make a <code>RewriteCond</code> apply
416 to several <code>RewriteRule</code>s, one possible technique is to
417 negate those conditions and use a [Skip] flag.</p>
421 <section id="flag_t"><title>T|type</title>
422 <p>Sets the MIME type with which the resulting response will be
423 sent. This has the same effect as the <directive
424 module="mod_mime">AddType</directive> directive.</p>
426 <p>For example, you might use the following technique to serve Perl
427 source code as plain text, if requested in a particular way:</p>
430 # Serve .pl files as plain text<br />
431 RewriteRule \.pl$ - [T=text/plain]
434 <p>Or, perhaps, if you have a camera that produces jpeg images without
435 file extensions, you could force those images to be served with the
436 correct MIME type by virtue of their file names:</p>
439 # Files with 'IMG' in the name are jpg images.<br />
440 RewriteRule IMG - [T=image/jpg]
443 <p>Please note that this is a trivial example, and could be better done
444 using <directive type="section" module="core">FilesMatch</directive>
445 instead. Always consider the alternate
446 solutions to a problem before resorting to rewrite, which will
447 invariably be a less efficient solution than the alternatives.</p>