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