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