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