]> granicus.if.org Git - apache/blob - docs/manual/rewrite/flags.xml
Rebuild.
[apache] / docs / manual / rewrite / flags.xml
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$ -->
5
6 <!--
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
13
14      http://www.apache.org/licenses/LICENSE-2.0
15
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.
21 -->
22
23 <manualpage metafile="flags.xml.meta">
24 <parentdocument href="./">Rewrite</parentdocument>
25
26   <title>RewriteRule Flags</title>
27
28 <summary>
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.</p>
32 </summary>
33
34 <seealso><a href="../mod/mod_rewrite.html">Module documentation</a></seealso>
35 <seealso><a href="intro.html">mod_rewrite introduction</a></seealso>
36 <seealso><a href="remapping.html">Redirection and remapping</a></seealso>
37 <seealso><a href="access.html">Controlling access</a></seealso>
38 <seealso><a href="vhosts.html">Virtual hosts</a></seealso>
39 <seealso><a href="proxy.html">Proxying</a></seealso>
40 <seealso><a href="rewritemap.html">Using RewriteMap</a></seealso>
41 <seealso><a href="advanced.html">Advanced techniques</a></seealso>
42 <seealso><a href="avoid.html">When not to use mod_rewrite</a></seealso>
43
44 <section id="introduction"><title>Introduction</title>
45 <p>A <directive module="mod_rewrite">RewriteRule</directive> can have
46 its behavior modified by one or more flags. Flags are included in
47 square brackets at the end of the rule, and multiple flags are separated
48 by commas.</p>
49 <highlight language="config">
50 RewriteRule pattern target [Flag1,Flag2,Flag3]
51 </highlight>
52
53 <p>Each flag (with a few exceptions) has a short form, such as
54 <code>CO</code>, as well as a longer form, such as <code>cookie</code>.
55 While it is most common to use
56 the short form, it is recommended that you familiarize yourself with the
57 long form, so that you remember what each flag is supposed to do.
58 Some flags take one or more arguments. Flags are not case sensitive.</p>
59
60 <p>Flags that alter metadata associated with the request (T=, H=, E=)
61 have no affect in per-directory and htaccess context, when a substitution
62 (other than '-') is performed during the same round of rewrite processing.
63 </p>
64
65 <p>Presented here are each of the available flags, along with an example
66 of how you might use them.</p>
67 </section>
68
69 <section id="flag_b"><title>B (escape backreferences)</title>
70 <p>The [B] flag instructs <directive
71 module="mod_rewrite">RewriteRule</directive> to escape non-alphanumeric
72 characters before applying the transformation.</p>
73 <p>In 2.4.26 and later, you can limit the escaping to specific characters
74 in backreferences by listing them: <code>[B=#?;]</code>. Note: The space
75 character can be used in the list of characters to escape, but it cannot be
76 the last character in the list.</p>
77
78 <p><code>mod_rewrite</code> has to unescape URLs before mapping them,
79 so backreferences are unescaped at the time they are applied.
80 Using the B flag, non-alphanumeric characters in backreferences
81 will be escaped. For example, consider the rule:</p>
82
83 <highlight language="config">
84 RewriteRule "^search/(.*)$" "/search.php?term=$1"
85 </highlight>
86
87 <p>Given a search term of 'x &amp; y/z', a browser will encode it as
88 'x%20%26%20y%2Fz', making the request 'search/x%20%26%20y%2Fz'. Without the B
89 flag, this rewrite rule will map to 'search.php?term=x &amp; y/z', which
90 isn't a valid URL, and so would be encoded as
91 <code>search.php?term=x%20&amp;y%2Fz=</code>, which is not what was intended.</p>
92
93 <p>With the B flag set on this same rule, the parameters are re-encoded
94 before being passed on to the output URL, resulting in a correct mapping to
95 <code>/search.php?term=x%20%26%20y%2Fz</code>.</p>
96
97 <highlight language="config">
98 RewriteRule "^search/(.*)$" "/search.php?term=$1" [B,PT]
99 </highlight>
100
101 <p>Note that you may also need to set <directive
102 module="core">AllowEncodedSlashes</directive> to <code>On</code> to get this
103 particular example to work, as httpd does not allow encoded slashes in URLs, and
104 returns a 404 if it sees one.</p>
105
106 <p>This escaping is particularly necessary in a proxy situation,
107 when the backend may break if presented with an unescaped URL.</p>
108
109 <p>An alternative to this flag is using a <directive module="mod_rewrite"
110 >RewriteCond</directive> to capture against %{THE_REQUEST} which will capture
111 strings in the encoded form.</p>
112 </section>
113
114 <section id="flag_bnp"><title>BNP|backrefnoplus (don't escape space to +)</title>
115 <p>The [BNP] flag instructs <directive
116 module="mod_rewrite">RewriteRule</directive> to escape the space character
117 in a backreference to %20 rather than '+'. Useful when the backreference
118 will be used in the path component rather than the query string.</p>
119
120 <p>This flag is available in version 2.4.26 and later.</p>
121
122 </section>
123
124 <section id="flag_c"><title>C|chain</title>
125 <p>The [C] or [chain] flag indicates that the <directive
126 module="mod_rewrite">RewriteRule</directive> is chained to the next
127 rule. That is, if the rule matches, then it is processed as usual and
128 control moves on to the next rule. However, if it does not match, then
129 the next rule, and any other rules that are chained together, are
130 skipped.</p>
131
132 </section>
133
134 <section id="flag_co"><title>CO|cookie</title>
135 <p>The [CO], or [cookie] flag, allows you to set a cookie when a
136 particular <directive module="mod_rewrite">RewriteRule</directive>
137 matches. The argument consists of three required fields and four optional
138 fields.</p>
139
140 <p>The full syntax for the flag, including all attributes, is as
141 follows:</p>
142
143 <example>
144 [CO=NAME:VALUE:DOMAIN:lifetime:path:secure:httponly]
145 </example>
146
147 <p>If a literal ':' character is needed in any of the cookie fields, an 
148 alternate syntax is available.  To opt-in to the alternate syntax, the cookie 
149 "Name" should be preceded with a ';' character, and field separators should be
150 specified as ';'.</p>
151
152 <example>
153 [CO=;NAME;VALUE:MOREVALUE;DOMAIN;lifetime;path;secure;httponly]
154 </example>
155
156 <p>You must declare a name, a value, and a domain for the cookie to be set.</p>
157
158 <dl>
159 <dt>Domain</dt>
160 <dd>The domain for which you want the cookie to be valid. This may be a
161 hostname, such as <code>www.example.com</code>, or it may be a domain,
162 such as <code>.example.com</code>. It must be at least two parts
163 separated by a dot. That is, it may not be merely <code>.com</code> or
164 <code>.net</code>. Cookies of that kind are forbidden by the cookie
165 security model.</dd>
166 </dl>
167
168 <p>You may optionally also set the following values:</p>
169
170 <dl>
171 <dt>Lifetime</dt>
172 <dd>The time for which the cookie will persist, in minutes.</dd>
173 <dd>A value of 0 indicates that the cookie will persist only for the
174 current browser session. This is the default value if none is
175 specified.</dd>
176
177 <dt>Path</dt>
178 <dd>The path, on the current website, for which the cookie is valid,
179 such as <code>/customers/</code> or <code>/files/download/</code>.</dd>
180 <dd>By default, this is set to <code>/</code> - that is, the entire
181 website.</dd>
182
183 <dt>Secure</dt>
184 <dd>If set to <code>secure</code>, <code>true</code>, or <code>1</code>,
185 the cookie will only be permitted to be translated via secure (https)
186 connections.</dd>
187
188 <dt>httponly</dt>
189 <dd>If set to <code>HttpOnly</code>, <code>true</code>, or
190 <code>1</code>, the cookie will have the <code>HttpOnly</code> flag set,
191 which means that the cookie is inaccessible to JavaScript code on
192 browsers that support this feature.</dd>
193 </dl>
194
195 <p>Consider this example:</p>
196
197 <highlight language="config">
198 RewriteEngine On
199 RewriteRule   "^/index\.html"   "-" [CO=frontdoor:yes:.example.com:1440:/]
200 </highlight>
201
202 <p>In the example give, the rule doesn't rewrite the request.
203 The "-" rewrite target tells mod_rewrite to pass the request
204 through unchanged. Instead, it sets a cookie
205 called 'frontdoor' to a value of 'yes'. The cookie is valid for any host
206 in the <code>.example.com</code> domain. It is set to expire in 1440
207 minutes (24 hours) and is returned for all URIs.</p>
208
209 </section>
210
211 <section id="flag_dpi"><title>DPI|discardpath</title>
212 <p>The DPI flag causes the PATH_INFO portion of the rewritten URI to be
213 discarded.</p>
214 <p>This flag is available in version 2.2.12 and later.</p>
215 <p>In per-directory context, the URI each <directive>RewriteRule</directive>
216 compares against is the concatenation of the current values of the URI
217 and PATH_INFO.</p>
218
219 <p>The current URI can be the initial URI as requested by the client, the
220 result of a previous round of mod_rewrite processing, or the result of
221 a prior rule in the current round of mod_rewrite processing.</p>
222
223 <p>In contrast, the PATH_INFO that is appended to the URI before each
224 rule reflects only the value of PATH_INFO before this round of
225 mod_rewrite processing. As a consequence, if large portions
226 of the URI are matched and copied into a substitution in multiple
227 <directive>RewriteRule</directive> directives, without regard for
228 which parts of the URI came from the current PATH_INFO, the final
229 URI may have multiple copies of PATH_INFO appended to it.</p>
230
231 <p>Use this flag on any substitution where the PATH_INFO that resulted
232 from the previous mapping of this request to the filesystem is not of
233 interest.  This flag permanently forgets the PATH_INFO established
234 before this round of mod_rewrite processing began. PATH_INFO will
235 not be recalculated until the current round of mod_rewrite processing
236 completes.  Subsequent rules during this round of processing will see
237 only the direct result of substitutions, without any PATH_INFO
238 appended.</p>
239 </section>
240
241 <section id="flag_e"><title>E|env</title>
242 <p>With the [E], or [env] flag, you can set the value of an environment
243 variable. Note that some environment variables may be set after the rule
244 is run, thus unsetting what you have set. See <a href="../env.html">the
245 Environment Variables document</a> for more details on how Environment
246 variables work.</p>
247
248 <p>The full syntax for this flag is:</p>
249
250 <highlight language="config">
251 [E=VAR:VAL]
252 [E=!VAR]
253 </highlight>
254
255 <p><code>VAL</code> may contain backreferences (<code>$N</code> or
256 <code>%N</code>) which are expanded.</p>
257
258 <p>Using the short form</p>
259
260 <example>
261 [E=VAR]
262 </example>
263
264 <p>you can set the environment variable named <code>VAR</code> to an
265 empty value.</p>
266
267 <p>The form</p>
268
269 <example>
270 [E=!VAR]
271 </example>
272
273 <p>allows to unset a previously set environment variable named
274 <code>VAR</code>.</p>
275
276 <p>Environment variables can then be used in a variety of
277 contexts, including CGI programs, other RewriteRule directives, or
278 CustomLog directives.</p>
279
280 <p>The following example sets an environment variable called 'image' to a
281 value of '1' if the requested URI is an image file. Then, that
282 environment variable is used to exclude those requests from the access
283 log.</p>
284
285 <highlight language="config">
286 RewriteRule "\.(png|gif|jpg)$"   "-" [E=image:1]
287 CustomLog   "logs/access_log"    combined env=!image
288 </highlight>
289
290 <p>Note that this same effect can be obtained using <directive
291 module="mod_setenvif">SetEnvIf</directive>. This technique is offered as
292 an example, not as a recommendation.</p>
293 </section>
294
295 <section id="flag_end"><title>END</title>
296 <p>Using the [END] flag terminates not only the current round of rewrite
297 processing (like [L]) but also prevents any subsequent rewrite
298 processing from occurring in per-directory (htaccess) context.</p>
299
300 <p>This does not apply to new requests resulting from external
301 redirects.</p>
302 </section>
303
304 <section id="flag_f"><title>F|forbidden</title>
305 <p>Using the [F] flag causes the server to return a 403 Forbidden status
306 code to the client. While the same behavior can be accomplished using
307 the <directive module="mod_access_compat">Deny</directive> directive, this
308 allows more flexibility in assigning a Forbidden status.</p>
309
310 <p>The following rule will forbid <code>.exe</code> files from being
311 downloaded from your server.</p>
312
313 <highlight language="config">
314 RewriteRule "\.exe"   "-" [F]
315 </highlight>
316
317 <p>This example uses the "-" syntax for the rewrite target, which means
318 that the requested URI is not modified. There's no reason to rewrite to
319 another URI, if you're going to forbid the request.</p>
320
321 <p>When using [F], an [L] is implied - that is, the response is returned
322 immediately, and no further rules are evaluated.</p>
323
324 </section>
325
326 <section id="flag_g"><title>G|gone</title>
327 <p>The [G] flag forces the server to return a 410 Gone status with the
328 response. This indicates that a resource used to be available, but is no
329 longer available.</p>
330
331 <p>As with the [F] flag, you will typically use the "-" syntax for the
332 rewrite target when using the [G] flag:</p>
333
334 <highlight language="config">
335 RewriteRule "oldproduct"   "-" [G,NC]
336 </highlight>
337
338 <p>When using [G], an [L] is implied - that is, the response is returned
339 immediately, and no further rules are evaluated.</p>
340
341 </section>
342
343 <section id="flag_h"><title>H|handler</title>
344 <p>Forces the resulting request to be handled with the specified
345 handler. For example, one might use this to force all files without a
346 file extension to be parsed by the php handler:</p>
347
348 <highlight language="config">
349 RewriteRule "!\."  "-" [H=application/x-httpd-php]
350 </highlight>
351
352 <p>
353 The regular expression above - <code>!\.</code> - will match any request
354 that does not contain the literal <code>.</code> character.
355 </p>
356
357 <p>This can be also used to force the handler based on some conditions.
358 For example, the following snippet used in per-server context allows
359 <code>.php</code> files to be <em>displayed</em> by <code>mod_php</code>
360 if they are requested with the <code>.phps</code> extension:</p>
361
362 <highlight language="config">
363 RewriteRule "^(/source/.+\.php)s$" "$1" [H=application/x-httpd-php-source]
364 </highlight>
365
366 <p>The regular expression above - <code>^(/source/.+\.php)s$</code> - will
367 match any request that starts with <code>/source/</code> followed by 1 or
368 n characters followed by <code>.phps</code> literally. The backreference
369 $1 referrers to the captured match within parenthesis of the regular
370 expression.</p>
371 </section>
372
373 <section id="flag_l"><title>L|last</title>
374 <p>The [L] flag causes <module>mod_rewrite</module> to stop processing
375 the rule set. In most contexts, this means that if the rule matches, no
376 further rules will be processed. This corresponds to the
377 <code>last</code> command in Perl, or the <code>break</code> command in
378 C. Use this flag to indicate that the current rule should be applied
379 immediately without considering further rules.</p>
380
381 <p>If you are using <directive
382 module="mod_rewrite">RewriteRule</directive> in either
383 <code>.htaccess</code> files or in
384 <directive type="section" module="core">Directory</directive> sections,
385 it is important to have some understanding of how the rules are
386 processed.  The simplified form of this is that once the rules have been
387 processed, the rewritten request is handed back to the URL parsing
388 engine to do what it may with it. It is possible that as the rewritten
389 request is handled, the <code>.htaccess</code> file or
390 <directive type="section" module="core">Directory</directive> section
391 may be encountered again, and thus the ruleset may be run again from the
392 start. Most commonly this will happen if one of the rules causes a
393 redirect - either internal or external - causing the request process to
394 start over.</p>
395
396 <p>It is therefore important, if you are using <directive
397 module="mod_rewrite">RewriteRule</directive> directives in one of these
398 contexts, that you take explicit steps to avoid rules looping, and not
399 count solely on the [L] flag to terminate execution of a series of
400 rules, as shown below.</p>
401
402 <p> An alternative flag, [END], can be used to terminate not only the
403 current round of rewrite processing but prevent any subsequent
404 rewrite processing from occurring in per-directory (htaccess)
405 context. This does not apply to new requests resulting from external
406 redirects.</p>
407
408 <p>The example given here will rewrite any request to
409 <code>index.php</code>, giving the original request as a query string
410 argument to <code>index.php</code>, however, the <directive
411 module="mod_rewrite">RewriteCond</directive> ensures that if the request
412 is already for <code>index.php</code>, the <directive
413 module="mod_rewrite">RewriteRule</directive> will be skipped.</p>
414
415 <highlight language="config">
416 RewriteBase "/"
417 RewriteCond "%{REQUEST_URI}" !=/index.php
418 RewriteRule "^(.*)"          "/index.php?req=$1" [L,PT]
419 </highlight>
420 </section>
421
422 <section id="flag_n"><title>N|next</title>
423 <p>
424 The [N] flag causes the ruleset to start over again from the top, using
425 the result of the ruleset so far as a starting point. Use
426 with extreme caution, as it may result in loop.
427 </p>
428 <p>
429 The [Next] flag could be used, for example, if you wished to replace a
430 certain string or letter repeatedly in a request. The example shown here
431 will replace A with B everywhere in a request, and will continue doing
432 so until there are no more As to be replaced.
433 </p>
434 <highlight language="config">
435 RewriteRule "(.*)A(.*)" "$1B$2" [N]
436 </highlight>
437 <p>You can think of this as a <code>while</code> loop: While this
438 pattern still matches (i.e., while the URI still contains an
439 <code>A</code>), perform this substitution (i.e., replace the
440 <code>A</code> with a <code>B</code>).</p>
441
442 <p>In 2.5.0 and later, this module returns an error after 10,000 iterations to
443 protect against unintended looping.  An alternative maximum number of
444 iterations can be specified by adding to the N flag.  </p>
445 <highlight language="config">
446 # Be willing to replace 1 character in each pass of the loop
447 RewriteRule "(.+)[&gt;&lt;;]$" "$1" [N=32000]
448 # ... or, give up if after 10 loops
449 RewriteRule "(.+)[&gt;&lt;;]$" "$1" [N=10]
450 </highlight>
451
452 </section>
453
454 <section id="flag_nc"><title>NC|nocase</title>
455 <p>Use of the [NC] flag causes the <directive
456 module="mod_rewrite">RewriteRule</directive> to be matched in a
457 case-insensitive manner. That is, it doesn't care whether letters appear
458 as upper-case or lower-case in the matched URI.</p>
459
460 <p>In the example below, any request for an image file will be proxied
461 to your dedicated image server. The match is case-insensitive, so that
462 <code>.jpg</code> and <code>.JPG</code> files are both acceptable, for
463 example.</p>
464
465 <highlight language="config">
466 RewriteRule "(.*\.(jpg|gif|png))$" "http://images.example.com$1" [P,NC]
467 </highlight>
468 </section>
469
470 <section id="flag_ne"><title>NE|noescape</title>
471 <p>By default, special characters, such as <code>&amp;</code> and
472 <code>?</code>, for example, will be converted to their hexcode
473 equivalent. Using the [NE] flag prevents that from happening.
474 </p>
475
476 <highlight language="config">
477 RewriteRule "^/anchor/(.+)" "/bigpage.html#$1" [NE,R]
478 </highlight>
479
480 <p>
481 The above example will redirect <code>/anchor/xyz</code> to
482 <code>/bigpage.html#xyz</code>. Omitting the [NE] will result in the #
483 being converted to its hexcode equivalent, <code>%23</code>, which will
484 then result in a 404 Not Found error condition.
485 </p>
486
487 </section>
488
489 <section id="flag_ns"><title>NS|nosubreq</title>
490 <p>Use of the [NS] flag prevents the rule from being used on
491 subrequests. For example, a page which is included using an SSI (Server
492 Side Include) is a subrequest, and you may want to avoid rewrites
493 happening on those subrequests. Also, when <module>mod_dir</module>
494 tries to find out information about possible directory default files
495 (such as <code>index.html</code> files), this is an internal
496 subrequest, and you often want to avoid rewrites on such subrequests.
497 On subrequests, it is not always useful, and can even cause errors, if
498 the complete set of rules are applied. Use this flag to exclude
499 problematic rules.</p>
500
501 <p>To decide whether or not to use this rule: if you prefix URLs with
502 CGI-scripts, to force them to be processed by the CGI-script, it's
503 likely that you will run into problems (or significant overhead)
504 on sub-requests. In these cases, use this flag.</p>
505
506 <p>
507 Images, javascript files, or css files, loaded as part of an HTML page,
508 are not subrequests - the browser requests them as separate HTTP
509 requests.
510 </p>
511 </section>
512
513 <section id="flag_p"><title>P|proxy</title>
514 <p>Use of the [P] flag causes the request to be handled by
515 <module>mod_proxy</module>, and handled via a proxy request. For
516 example, if you wanted all image requests to be handled by a back-end
517 image server, you might do something like the following:</p>
518
519 <highlight language="config">
520 RewriteRule "/(.*)\.(jpg|gif|png)$" "http://images.example.com/$1.$2" [P]
521 </highlight>
522
523 <p>Use of the [P] flag implies [L] - that is, the request is immediately
524 pushed through the proxy, and any following rules will not be
525 considered.</p>
526
527 <p>
528 You must make sure that the substitution string is a valid URI
529 (typically starting with <code>http://</code><em>hostname</em>) which can be
530 handled by the <module>mod_proxy</module>. If not, you will get an
531 error from the proxy module. Use this flag to achieve a
532 more powerful implementation of the <directive
533 module="mod_proxy">ProxyPass</directive> directive,
534 to map remote content into the namespace of the local server.</p>
535
536 <note type="warning">
537 <title>Security Warning</title>
538 <p>Take care when constructing the target URL of the rule, considering
539 the security impact from allowing the client influence over the set of
540 URLs to which your server will act as a proxy.  Ensure that the scheme
541 and hostname part of the URL is either fixed, or does not allow the
542 client undue influence.</p>
543 </note>
544
545 <note type="warning">
546 <title>Performance warning</title>
547 <p>Using this flag triggers the use of <module>mod_proxy</module>, without handling of persistent connections. This
548 means the performance of your proxy will be better if you set it up with <directive module="mod_proxy">ProxyPass</directive> or
549 <directive module="mod_proxy">ProxyPassMatch</directive></p>
550 <p>This is because this flag triggers the use of the default worker, which does not handle connection pooling.</p>
551 <p>Avoid using this flag and prefer those directives, whenever you can.</p>
552 </note>
553
554 <p>Note: <module>mod_proxy</module> must be enabled in order
555 to use this flag.</p>
556
557 </section>
558
559 <section id="flag_pt"><title>PT|passthrough</title>
560
561 <p>
562 The target (or substitution string) in a RewriteRule is assumed to be a
563 file path, by default. The use of the [PT] flag causes it to be treated
564 as a URI instead. That is to say, the
565 use of the [PT] flag causes the result of the <directive
566 module="mod_rewrite">RewriteRule</directive> to be passed back through
567 URL mapping, so that location-based mappings, such as <directive
568 module="mod_alias">Alias</directive>, <directive
569 module="mod_alias">Redirect</directive>, or <directive
570 module="mod_alias">ScriptAlias</directive>, for example, might have a
571 chance to take effect.
572 </p>
573
574 <p>
575 If, for example, you have an
576 <directive module="mod_alias">Alias</directive>
577 for /icons, and have a <directive
578 module="mod_rewrite">RewriteRule</directive> pointing there, you should
579 use the [PT] flag to ensure that the
580 <directive module="mod_alias">Alias</directive> is evaluated.
581 </p>
582
583 <highlight language="config">
584 Alias "/icons" "/usr/local/apache/icons"
585 RewriteRule "/pics/(.+)\.jpg$" "/icons/$1.gif" [PT]
586 </highlight>
587
588 <p>
589 Omission of the [PT] flag in this case will cause the Alias to be
590 ignored, resulting in a 'File not found' error being returned.
591 </p>
592
593 <p>The <code>PT</code> flag implies the <code>L</code> flag:
594 rewriting will be stopped in order to pass the request to
595 the next phase of processing.</p>
596
597 <p>Note that the <code>PT</code> flag is implied in per-directory
598 contexts such as
599 <directive type="section" module="core">Directory</directive> sections
600 or in <code>.htaccess</code> files. The only way to circumvent that
601 is to rewrite to <code>-</code>.</p>
602
603 </section>
604
605 <section id="flag_qsa"><title>QSA|qsappend</title>
606 <p>
607 When the replacement URI contains a query string, the default behavior
608 of <directive module="mod_rewrite">RewriteRule</directive> is to discard
609 the existing query string, and replace it with the newly generated one.
610 Using the [QSA] flag causes the query strings to be combined.
611 </p>
612
613 <p>Consider the following rule:</p>
614
615 <highlight language="config">
616 RewriteRule "/pages/(.+)" "/page.php?page=$1" [QSA]
617 </highlight>
618
619 <p>With the [QSA] flag, a request for <code>/pages/123?one=two</code> will be
620 mapped to <code>/page.php?page=123&amp;one=two</code>. Without the [QSA]
621 flag, that same request will be mapped to
622 <code>/page.php?page=123</code> - that is, the existing query string
623 will be discarded.
624 </p>
625 </section>
626
627 <section id="flag_qsd"><title>QSD|qsdiscard</title>
628 <p>
629 When the requested URI contains a query string, and the target URI does
630 not, the default behavior of <directive
631 module="mod_rewrite">RewriteRule</directive> is to copy that query
632 string to the target URI. Using the [QSD] flag causes the query string
633 to be discarded.
634 </p>
635
636 <p>This flag is available in version 2.4.0 and later.</p>
637
638 <p>
639 Using [QSD] and [QSA] together will result in [QSD] taking precedence.
640 </p>
641
642 <p>
643 If the target URI has a query string, the default behavior will be
644 observed - that is, the original query string will be discarded and
645 replaced with the query string in the <code>RewriteRule</code> target
646 URI.
647 </p>
648
649 </section>
650
651 <section id="flag_qsl"><title>QSL|qslast</title>
652 <p>
653 By default, the first (left-most) question mark in the substitution
654 delimits the path from the query string.  Using the [QSL] flag instructs
655 <directive module="mod_rewrite">RewriteRule</directive> to instead split
656 the two components using the last (right-most) question mark.  </p>
657
658 <p>
659 This is useful when mapping to files that have literal question marks in 
660 their filename.  If no query string is used in the substitution, 
661 a question mark can be appended to it in combination with this flag.  </p>
662
663 <p> This flag is available in version 2.4.19 and later.</p>
664
665 </section>
666
667
668 <section id="flag_r"><title>R|redirect</title>
669 <p>
670 Use of the [R] flag causes a HTTP redirect to be issued to the browser.
671 If a fully-qualified URL is specified (that is, including
672 <code>http://servername/</code>) then a redirect will be issued to that
673 location. Otherwise, the current protocol, servername, and port number
674 will be used to generate the URL sent with the redirect.
675 </p>
676
677 <p>
678 <em>Any</em> valid HTTP response  status code may be specified,
679 using the syntax [R=305], with a 302 status code being used by
680 default if none is specified. The status code specified need not
681 necessarily be a redirect (3xx) status code. However,
682 if a status code is outside the redirect range (300-399) then the
683 substitution string is dropped entirely, and rewriting is stopped as if
684 the <code>L</code> were used.</p>
685
686 <p>In addition to response status codes, you may also specify redirect
687 status using their symbolic names: <code>temp</code> (default),
688 <code>permanent</code>, or <code>seeother</code>.</p>
689
690 <p>
691 You will almost always want to use [R] in conjunction with [L] (that is,
692 use [R,L]) because on its own, the [R] flag prepends
693 <code>http://thishost[:thisport]</code> to the URI, but then passes this
694 on to the next rule in the ruleset, which can often result in 'Invalid
695 URI in request' warnings.
696 </p>
697
698 </section>
699
700 <section id="flag_s"><title>S|skip</title>
701 <p>The [S] flag is used to skip rules that you don't want to run. The
702 syntax of the skip flag is [S=<em>N</em>], where <em>N</em> signifies
703 the number of rules to skip (provided the <directive module="mod_rewrite">
704 RewriteRule</directive> and any preceding <directive module="mod_rewrite">
705 RewriteCond</directive> directives match). This can be thought of as a
706 <code>goto</code> statement in your rewrite ruleset. In the following
707 example, we only want to run the <directive module="mod_rewrite">
708 RewriteRule</directive> if the requested URI doesn't correspond with an
709 actual file.</p>
710
711 <highlight language="config">
712 # Is the request for a non-existent file?
713 RewriteCond "%{REQUEST_FILENAME}" !-f
714 RewriteCond "%{REQUEST_FILENAME}" !-d
715 # If so, skip these two RewriteRules
716 RewriteRule ".?"                  "-" [S=2]
717
718 RewriteRule "(.*\.gif)"           "images.php?$1"
719 RewriteRule "(.*\.html)"          "docs.php?$1"
720 </highlight>
721
722 <p>This technique is useful because a <directive
723 module="mod_rewrite">RewriteCond</directive> only applies to the
724 <directive module="mod_rewrite">RewriteRule</directive> immediately
725 following it. Thus, if you want to make a <code>RewriteCond</code> apply
726 to several <code>RewriteRule</code>s, one possible technique is to
727 negate those conditions and add a <code>RewriteRule</code> with a [Skip] flag. You can
728 use this to make pseudo if-then-else constructs: The last rule of
729 the then-clause becomes <code>skip=N</code>, where N is the
730 number of rules in the else-clause:</p>
731 <highlight language="config">
732 # Does the file exist?
733 RewriteCond "%{REQUEST_FILENAME}" !-f
734 RewriteCond "%{REQUEST_FILENAME}" !-d
735 # Create an if-then-else construct by skipping 3 lines if we meant to go to the &quot;else&quot; stanza.
736 RewriteRule ".?"                  "-" [S=3]
737
738 # IF the file exists, then:
739     RewriteRule "(.*\.gif)"  "images.php?$1"
740     RewriteRule "(.*\.html)" "docs.php?$1"
741     # Skip past the &quot;else&quot; stanza.
742     RewriteRule ".?"         "-" [S=1]
743 # ELSE...
744     RewriteRule "(.*)"       "404.php?file=$1"
745 # END
746 </highlight>
747
748 <p>It is probably easier to accomplish this kind of configuration using
749 the <directive type="section">If</directive>, <directive
750 type="section">ElseIf</directive>, and <directive
751 type="section">Else</directive> directives instead.</p>
752
753 </section>
754
755 <section id="flag_t"><title>T|type</title>
756 <p>Sets the MIME type with which the resulting response will be
757 sent. This has the same effect as the <directive
758 module="mod_mime">AddType</directive> directive.</p>
759
760 <p>For example, you might use the following technique to serve Perl
761 source code as plain text, if requested in a particular way:</p>
762
763 <highlight language="config">
764 # Serve .pl files as plain text
765 RewriteRule "\.pl$"  "-" [T=text/plain]
766 </highlight>
767
768 <p>Or, perhaps, if you have a camera that produces jpeg images without
769 file extensions, you could force those images to be served with the
770 correct MIME type by virtue of their file names:</p>
771
772 <highlight language="config">
773 # Files with 'IMG' in the name are jpg images.
774 RewriteRule "IMG"  "-" [T=image/jpg]
775 </highlight>
776
777 <p>Please note that this is a trivial example, and could be better done
778 using <directive type="section" module="core">FilesMatch</directive>
779 instead. Always consider the alternate
780 solutions to a problem before resorting to rewrite, which will
781 invariably be a less efficient solution than the alternatives.</p>
782
783 <p>
784 If used in per-directory context, use only <code>-</code> (dash)
785 as the substitution <em>for the entire round of mod_rewrite processing</em>,
786 otherwise the MIME-type set with this flag is lost due to an internal
787 re-processing (including subsequent rounds of mod_rewrite processing).
788 The <code>L</code> flag can be useful in this context to end the
789 <em>current</em> round of mod_rewrite processing.</p>
790
791 </section>
792
793 </manualpage>