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