]> granicus.if.org Git - apache/blob - docs/manual/mod/mod_rewrite.xml
Quote {Alias,Redirect,ScriptAlias}{,Match} arguments.
[apache] / docs / manual / mod / mod_rewrite.xml
1 <?xml version="1.0"?>
2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.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 <modulesynopsis metafile="mod_rewrite.xml.meta">
24
25 <name>mod_rewrite</name>
26
27 <description>Provides a rule-based rewriting engine to rewrite requested
28 URLs on the fly</description>
29
30 <status>Extension</status>
31 <sourcefile>mod_rewrite.c</sourcefile>
32 <identifier>rewrite_module</identifier>
33
34 <summary>
35         <p>The <module>mod_rewrite</module> module uses a rule-based rewriting
36       engine, based on a PCRE regular-expression parser, to rewrite requested URLs on
37       the fly. By default, <module>mod_rewrite</module> maps a URL to a filesystem
38       path. However, it can also be used to redirect one URL to another URL, or
39       to invoke an internal proxy fetch.</p>
40       <p><module>mod_rewrite</module> provides a flexible and powerful way to
41       manipulate URLs using an unlimited number of rules. Each rule can have an
42       unlimited number of attached rule conditions, to allow you to rewrite URL
43       based on server variables, environment variables, HTTP headers, or time
44       stamps.</p>
45       <p><module>mod_rewrite</module> operates on the full URL path, including the
46       path-info section. A rewrite rule can be invoked in
47       <code>httpd.conf</code> or in <code>.htaccess</code>. The path generated
48       by a rewrite rule can include a query string, or can lead to internal
49       sub-processing, external request redirection, or internal proxy
50       throughput.</p>
51
52       <p>Further details, discussion, and examples, are provided in the
53       <a href="../rewrite/">detailed mod_rewrite documentation</a>.</p>
54 </summary>
55
56 <section id="logging"><title>Logging</title>
57
58     <p><module>mod_rewrite</module> offers detailed logging of its actions
59     at the <code>trace1</code> to <code>trace8</code> log levels. The
60     log level can be set specifically for <module>mod_rewrite</module>
61     using the <directive module="core">LogLevel</directive> directive: Up to
62     level <code>debug</code>, no actions are logged, while <code>trace8</code>
63     means that practically all actions are logged.</p>
64
65     <note>
66       Using a high trace log level for <module>mod_rewrite</module>
67       will slow down your Apache HTTP Server dramatically! Use a log
68       level higher than <code>trace2</code> only for debugging!
69     </note>
70
71     <example><title>Example</title>
72     <highlight language="config">
73       LogLevel alert rewrite:trace3
74       </highlight>
75     </example>
76
77     <note><title>RewriteLog</title>
78       <p>Those familiar with earlier versions of
79       <module>mod_rewrite</module> will no doubt be looking for the
80       <code>RewriteLog</code> and <code>RewriteLogLevel</code>
81       directives. This functionality has been completely replaced by the
82       new per-module logging configuration mentioned above.
83       </p>
84
85       <p>To get just the <module>mod_rewrite</module>-specific log
86       messages, pipe the log file through grep:</p>
87     <example>
88     tail -f error_log|fgrep '[rewrite:'
89     </example>
90     </note>
91
92 </section>
93
94 <directivesynopsis>
95 <name>RewriteEngine</name>
96 <description>Enables or disables runtime rewriting engine</description>
97 <syntax>RewriteEngine on|off</syntax>
98 <default>RewriteEngine off</default>
99 <contextlist><context>server config</context><context>virtual host</context>
100 <context>directory</context><context>.htaccess</context></contextlist>
101 <override>FileInfo</override>
102
103 <usage>
104
105       <p>The <directive>RewriteEngine</directive> directive enables or
106       disables the runtime rewriting engine. If it is set to
107       <code>off</code> this module does no runtime processing at
108       all. It does not even update the <code>SCRIPT_URx</code>
109       environment variables.</p>
110
111       <p>Use this directive to disable rules in a particular context,
112       rather than commenting out all the <directive
113       module="mod_rewrite">RewriteRule</directive> directives.</p>
114
115       <p>Note that rewrite configurations are not
116       inherited by virtual hosts. This means that you need to have a
117       <code>RewriteEngine on</code> directive for each virtual host
118       in which you wish to use rewrite rules.</p>
119
120       <p><directive>RewriteMap</directive> directives of the type <code>prg</code>
121       are not started during server initialization if they're defined in a
122       context that does not have <directive>RewriteEngine</directive>  set to
123       <code>on</code></p>
124 </usage>
125
126 </directivesynopsis>
127
128 <directivesynopsis>
129 <name>RewriteOptions</name>
130 <description>Sets some special options for the rewrite engine</description>
131 <syntax>RewriteOptions <var>Options</var></syntax>
132 <contextlist><context>server config</context><context>virtual host</context>
133 <context>directory</context><context>.htaccess</context></contextlist>
134 <override>FileInfo</override>
135 <usage>
136
137       <p>The <directive>RewriteOptions</directive> directive sets some
138       special options for the current per-server or per-directory
139       configuration. The <em>Option</em> string can currently
140       only be one of the following:</p>
141
142       <dl>
143       <dt><code>Inherit</code></dt>
144       <dd>
145
146       <p>This forces the current configuration to inherit the
147       configuration of the parent. In per-virtual-server context,
148       this means that the maps, conditions and rules of the main
149       server are inherited. In per-directory context this means
150       that conditions and rules of the parent directory's
151       <code>.htaccess</code> configuration or
152       <directive type="section" module="core">Directory</directive>
153       sections are inherited. The inherited rules are virtually copied
154       to the section where this directive is being used. If used in
155       combination with local rules, the inherited rules are copied behind
156       the local rules. The position of this directive - below or above
157       of local rules - has no influence on this behavior. If local
158       rules forced the rewriting to stop, the inherited rules won't
159       be processed.</p>
160
161       <note type="warning">
162       Rules inherited from the parent scope are applied
163       <strong>after</strong> rules specified in the child scope.
164       </note>
165       </dd>
166
167       <dt><code>InheritBefore</code></dt>
168       <dd>
169       <p> Like <code>Inherit</code> above, but the rules from the parent scope
170       are applied <strong>before</strong> rules specified in the child scope.<br />
171       Available in Apache HTTP Server 2.3.10 and later.</p>
172       </dd>
173
174       <dt><code>InheritDown</code></dt>
175       <dd>
176
177       <p>If this option is enabled, all child configurations will inherit
178       the configuration of the current configuration. It is equivalent to
179       specifying <code>RewriteOptions Inherit</code> in all child
180       configurations. See the <code>Inherit</code> option for more details
181       on how the parent-child relationships are handled.<br />
182       Available in Apache HTTP Server 2.4.8 and later.</p>
183       </dd>
184
185       <dt><code>InheritDownBefore</code></dt>
186       <dd>
187
188       <p>Like <code>InheritDown</code> above, but the rules from the current
189       scope are applied <strong>before</strong> rules specified in any child's
190       scope.<br />
191       Available in Apache HTTP Server 2.4.8 and later.</p>
192       </dd>
193
194       <dt><code>IgnoreInherit</code></dt>
195       <dd>
196
197       <p>This option forces the current and child configurations to ignore
198       all rules that would be inherited from a parent specifying
199       <code>InheritDown</code> or <code>InheritDownBefore</code>.<br />
200       Available in Apache HTTP Server 2.4.8 and later.</p>
201       </dd>
202
203       <dt><code>AllowNoSlash</code></dt>
204       <dd>
205       <p>By default, <module>mod_rewrite</module> will ignore URLs that map to a
206       directory on disk but lack a trailing slash, in the expectation that
207       the <module>mod_dir</module> module will issue the client with a redirect to
208       the canonical URL with a trailing slash.</p>
209       
210       <p>When the <directive module="mod_dir">DirectorySlash</directive> directive
211       is set to off, the <code>AllowNoSlash</code> option can be enabled to ensure
212       that rewrite rules are no longer ignored. This option makes it possible to
213       apply rewrite rules within .htaccess files that match the directory without
214       a trailing slash, if so desired.<br />
215       Available in Apache HTTP Server 2.4.0 and later.</p>
216       </dd>
217
218       <dt><code>AllowAnyURI</code></dt>
219       <dd>
220
221       <p>When <directive module="mod_rewrite">RewriteRule</directive>
222       is used in <code>VirtualHost</code> or server context with
223       version 2.2.22 or later of httpd, <module>mod_rewrite</module>
224       will only process the rewrite rules if the request URI is a <a
225       href="directive-dict.html#Syntax">URL-path</a>.  This avoids
226       some security issues where particular rules could allow
227       "surprising" pattern expansions (see <a
228       href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2011-3368">CVE-2011-3368</a>
229       and <a
230       href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2011-4317">CVE-2011-4317</a>).
231       To lift the restriction on matching a URL-path, the
232       <code>AllowAnyURI</code> option can be enabled, and
233       <module>mod_rewrite</module> will apply the rule set to any
234       request URI string, regardless of whether that string matches
235       the URL-path grammar required by the HTTP specification.<br />
236       Available in Apache HTTP Server 2.4.3 and later.</p>
237
238       <note type="warning">
239       <title>Security Warning</title> 
240
241       <p>Enabling this option will make the server vulnerable to
242       security issues if used with rewrite rules which are not
243       carefully authored.  It is <strong>strongly recommended</strong>
244       that this option is not used.  In particular, beware of input
245       strings containing the '<code>@</code>' character which could
246       change the interpretation of the transformed URI, as per the
247       above CVE names.</p>
248       </note>
249       </dd>
250
251       <dt><code>MergeBase</code></dt>
252       <dd>
253
254       <p>With this option, the value of <directive module="mod_rewrite"
255       >RewriteBase</directive> is copied from where it's explicitly defined
256       into any sub-directory or sub-location that doesn't define its own
257       <directive module="mod_rewrite">RewriteBase</directive>. This was the
258       default behavior in 2.4.0 through 2.4.3, and the flag to restore it is
259       available Apache HTTP Server 2.4.4 and later.</p>
260       </dd>
261
262       <dt><code>IgnoreContextInfo</code></dt>
263       <dd>
264
265       <p>When a relative substitution is made
266          in directory (htaccess) context and <directive module="mod_rewrite"
267          >RewriteBase</directive> has not been set, this module uses some
268          extended URL and filesystem context information to change the 
269          relative substitution back into a URL. Modules such as 
270          <module>mod_userdir</module> and <module>mod_alias</module>
271          supply this extended context info.  </p>
272       </dd>
273
274       </dl>
275 </usage>
276
277 </directivesynopsis>
278
279 <directivesynopsis>
280 <name>RewriteMap</name>
281 <description>Defines a mapping function for key-lookup</description>
282 <syntax>RewriteMap <em>MapName</em> <em>MapType</em>:<em>MapSource</em>
283     <em>MapTypeOptions</em>
284 </syntax>
285 <contextlist><context>server config</context><context>virtual host</context>
286 </contextlist>
287
288 <usage>
289       <p>The <directive>RewriteMap</directive> directive defines a
290       <em>Rewriting Map</em> which can be used inside rule
291       substitution strings by the mapping-functions to
292       insert/substitute fields through a key lookup. The source of
293       this lookup can be of various types.</p>
294
295       <p>The <a id="mapfunc" name="mapfunc"><em>MapName</em></a> is
296       the name of the map and will be used to specify a
297       mapping-function for the substitution strings of a rewriting
298       rule via one of the following constructs:</p>
299
300       <p class="indent">
301         <strong><code>${</code> <em>MapName</em> <code>:</code>
302         <em>LookupKey</em> <code>}</code><br />
303          <code>${</code> <em>MapName</em> <code>:</code>
304         <em>LookupKey</em> <code>|</code> <em>DefaultValue</em>
305         <code>}</code></strong>
306       </p>
307
308       <p>When such a construct occurs, the map <em>MapName</em> is
309       consulted and the key <em>LookupKey</em> is looked-up. If the
310       key is found, the map-function construct is substituted by
311       <em>SubstValue</em>. If the key is not found then it is
312       substituted by <em>DefaultValue</em> or by the empty string
313       if no <em>DefaultValue</em> was specified. Empty values
314       behave as if the key was absent, therefore it is not possible
315       to distinguish between empty-valued keys and absent keys.</p>
316
317       <p>For example, you might define a
318       <directive>RewriteMap</directive> as:</p>
319
320       <highlight language="config">
321       RewriteMap examplemap txt:/path/to/file/map.txt
322       </highlight>
323
324       <p>You would then be able to use this map in a
325       <directive>RewriteRule</directive> as follows:</p>
326
327       <highlight language="config">
328       RewriteRule ^/ex/(.*) ${examplemap:$1}
329       </highlight>
330
331       <p>The meaning of the <em>MapTypeOptions</em> argument depends on
332       particular <em>MapType</em>. See the
333       <a href="../rewrite/rewritemap.html">Using RewriteMap</a> for
334       more information.</p>
335
336       <p>The following combinations for <em>MapType</em> and
337       <em>MapSource</em> can be used:</p>
338
339     <dl>
340
341     <dt>txt</dt>
342         <dd>A plain text file containing space-separated key-value
343         pairs, one per line. (<a href="../rewrite/rewritemap.html#txt">Details ...</a>)</dd>
344
345     <dt>rnd</dt>
346         <dd>Randomly selects an entry from a plain text file (<a href="../rewrite/rewritemap.html#rnd">Details ...</a>)</dd>
347
348     <dt>dbm</dt>
349         <dd>Looks up an entry in a dbm file containing name, value
350         pairs. Hash is constructed from a plain text file format using
351         the <code><a href="../programs/httxt2dbm.html">httxt2dbm</a></code>
352         utility.  (<a href="../rewrite/rewritemap.html#dbm">Details ...</a>)</dd>
353
354     <dt>int</dt>
355         <dd>One of the four available internal functions provided by
356         <code>RewriteMap</code>: toupper, tolower, escape or
357         unescape. (<a href="../rewrite/rewritemap.html#int">Details ...</a>)</dd>
358
359     <dt>prg</dt>
360         <dd>Calls an external program or script to process the
361         rewriting. (<a href="../rewrite/rewritemap.html#prg">Details ...</a>)</dd>
362
363     <dt>dbd or fastdbd</dt>
364         <dd>A SQL SELECT statement to be performed to look up the
365         rewrite target. (<a href="../rewrite/rewritemap.html#dbd">Details ...</a>)</dd>
366     </dl>
367
368     <p>Further details, and numerous examples, may be found in the <a
369     href="../rewrite/rewritemap.html">RewriteMap HowTo</a></p>
370
371 </usage>
372 </directivesynopsis>
373
374 <directivesynopsis>
375 <name>RewriteBase</name>
376 <description>Sets the base URL for per-directory rewrites</description>
377 <syntax>RewriteBase <em>URL-path</em></syntax>
378 <default>None</default>
379 <contextlist><context>directory</context><context>.htaccess</context>
380 </contextlist>
381 <override>FileInfo</override>
382
383 <usage>
384       <p>The <directive>RewriteBase</directive> directive specifies the
385       URL prefix to be used for per-directory (htaccess) 
386       <directive>RewriteRule</directive> directives that substitute a relative
387       path.</p>
388       <p> This directive is <em>required</em> when you use a relative path
389       in a substitution in per-directory (htaccess) context unless either
390       of the following conditions are true:</p>
391       <ul>
392           <li> The original request, and the substitution, are underneath the 
393                <directive module="core">DocumentRoot</directive>
394                (as opposed to reachable by other means, such as 
395                <directive module="mod_alias">Alias</directive>).</li>
396           <li> The <em>filesystem</em> path to the directory containing the
397                <directive>RewriteRule</directive>, suffixed by the relative 
398                substitution is also valid as a URL path on the server 
399                (this is rare).</li>
400           <li> In Apache HTTP Server 2.4.11 and later, this directive may be 
401                 omitted when the request is mapped via 
402                 <directive module="mod_alias">Alias</directive>
403                 or <module>mod_userdir</module>.</li>
404       </ul>
405
406 <p> In the example below, <directive>RewriteBase</directive> is necessary
407     to avoid rewriting to http://example.com/opt/myapp-1.2.3/welcome.html
408     since the resource was not relative to the document root.  This 
409     misconfiguration would normally cause the server to look for an "opt"
410     directory under the document root.</p>
411 <highlight language="config">
412 DocumentRoot /var/www/example.com
413 AliasMatch "^/myapp" "/opt/myapp-1.2.3"
414 &lt;Directory "/opt/myapp-1.2.3"&gt;
415     RewriteEngine On
416     RewriteBase /myapp/
417     RewriteRule ^index\.html$  welcome.html 
418 &lt;/Directory&gt;
419 </highlight>
420
421 </usage>
422
423 </directivesynopsis>
424
425 <directivesynopsis>
426 <name>RewriteCond</name>
427 <description>Defines a condition under which rewriting will take place
428 </description>
429 <syntax> RewriteCond
430       <em>TestString</em> <em>CondPattern</em></syntax>
431 <contextlist><context>server config</context><context>virtual host</context>
432 <context>directory</context><context>.htaccess</context></contextlist>
433 <override>FileInfo</override>
434
435 <usage>
436       <p>The <directive>RewriteCond</directive> directive defines a
437       rule condition. One or more <directive>RewriteCond</directive>
438       can precede a <directive module="mod_rewrite">RewriteRule</directive>
439       directive. The following rule is then only used if both
440       the current state of the URI matches its pattern, <strong
441       >and</strong> if these conditions are met.</p>
442
443       <p><em>TestString</em> is a string which can contain the
444       following expanded constructs in addition to plain text:</p>
445
446       <ul>
447         <li>
448           <strong>RewriteRule backreferences</strong>: These are
449           backreferences of the form <strong><code>$N</code></strong>
450           (0 &lt;= N &lt;= 9). $1 to $9 provide access to the grouped
451           parts (in parentheses) of the pattern, from the
452           <code>RewriteRule</code> which is subject to the current
453           set of <code>RewriteCond</code> conditions. $0 provides
454           access to the whole string matched by that pattern.
455         </li>
456         <li>
457           <strong>RewriteCond backreferences</strong>: These are
458           backreferences of the form <strong><code>%N</code></strong>
459           (0 &lt;= N &lt;= 9). %1 to %9 provide access to the grouped
460           parts (again, in parentheses) of the pattern, from the last matched
461           <code>RewriteCond</code> in the current set
462           of conditions. %0 provides access to the whole string matched by
463           that pattern.
464         </li>
465         <li>
466           <strong>RewriteMap expansions</strong>: These are
467           expansions of the form <strong><code
468           >${mapname:key|default}</code></strong>.
469           See <a href="#mapfunc">the documentation for
470           RewriteMap</a> for more details.
471         </li>
472         <li>
473           <strong>Server-Variables</strong>: These are variables of
474           the form
475             <strong><code>%{</code> <em>NAME_OF_VARIABLE</em>
476             <code>}</code></strong>
477           where <em>NAME_OF_VARIABLE</em> can be a string taken
478           from the following list:
479
480           <table>
481           <columnspec><column width=".3"/><column width=".3"/>
482            <column width=".3"/></columnspec>
483             <tr>
484               <th>HTTP headers:</th> <th>connection &amp; request:</th> <th></th>
485             </tr>
486
487             <tr>
488               <td>
489                  HTTP_ACCEPT<br />
490                  HTTP_COOKIE<br />
491                  HTTP_FORWARDED<br />
492                  HTTP_HOST<br />
493                  HTTP_PROXY_CONNECTION<br />
494                  HTTP_REFERER<br />
495                  HTTP_USER_AGENT<br />
496               </td>
497
498               <td>
499                  AUTH_TYPE<br />
500                  CONN_REMOTE_ADDR<br />
501                  CONTEXT_PREFIX<br />
502                  CONTEXT_DOCUMENT_ROOT<br />
503                  IPV6<br />
504                  PATH_INFO<br />
505                  QUERY_STRING<br />
506                  REMOTE_ADDR<br />
507                  REMOTE_HOST<br />
508                  REMOTE_IDENT<br />
509                  REMOTE_PORT<br />
510                  REMOTE_USER<br />
511                  REQUEST_METHOD<br />
512                  SCRIPT_FILENAME<br />
513               </td>
514
515               <td></td>
516             </tr>
517
518             <tr>
519               <th>server internals:</th> <th>date and time:</th> <th>specials:</th>
520             </tr>
521
522             <tr>
523               <td>
524                  DOCUMENT_ROOT<br />
525                  SCRIPT_GROUP<br />
526                  SCRIPT_USER<br />
527                  SERVER_ADDR<br />
528                  SERVER_ADMIN<br />
529                  SERVER_NAME<br />
530                  SERVER_PORT<br />
531                  SERVER_PROTOCOL<br />
532                  SERVER_SOFTWARE<br />
533               </td>
534
535               <td>
536                  TIME_YEAR<br />
537                  TIME_MON<br />
538                  TIME_DAY<br />
539                  TIME_HOUR<br />
540                  TIME_MIN<br />
541                  TIME_SEC<br />
542                  TIME_WDAY<br />
543                  TIME<br />
544               </td>
545
546               <td>
547                  API_VERSION<br />
548                  CONN_REMOTE_ADDR<br />
549                  HTTPS<br />
550                  IS_SUBREQ<br />
551                  REMOTE_ADDR<br />
552                  REQUEST_FILENAME<br />
553                  REQUEST_SCHEME<br />
554                  REQUEST_URI<br />
555                  THE_REQUEST<br />
556               </td>
557             </tr>
558           </table>
559
560                 <p>These variables all
561                 correspond to the similarly named HTTP
562                 MIME-headers, C variables of the Apache HTTP Server or
563                 <code>struct tm</code> fields of the Unix system.
564                 Most are documented <a href="../expr.html#vars">here</a>
565                 or elsewhere in the Manual or in the CGI specification.</p>
566
567                 <p>SERVER_NAME and SERVER_PORT depend on the values of
568                 <directive module="core">UseCanonicalName</directive> and
569                 <directive module="core">UseCanonicalPhysicalPort</directive>
570                 respectively.</p>
571
572                 <p>Those that are special to mod_rewrite include those below.</p>
573         <note>
574                 <dl>
575                   <dt><code>API_VERSION</code></dt>
576
577                   <dd>This is the version of the Apache httpd module API
578                   (the internal interface between server and
579                   module) in the current httpd build, as defined in
580                   include/ap_mmn.h. The module API version
581                   corresponds to the version of Apache httpd in use (in
582                   the release version of Apache httpd 1.3.14, for
583                   instance, it is 19990320:10), but is mainly of
584                   interest to module authors.</dd>
585
586                   <dt><code>CONN_REMOTE_ADDR</code></dt>
587
588                   <dd>Since 2.4.8: The peer IP address of the connection (see the
589                   <module>mod_remoteip</module> module).</dd>
590
591                   <dt><code>HTTPS</code></dt>
592
593                   <dd>Will contain the text "on" if the connection is
594                   using SSL/TLS, or "off" otherwise.  (This variable
595                   can be safely used regardless of whether or not
596                   <module>mod_ssl</module> is loaded).</dd>
597
598                   <dt><code>IS_SUBREQ</code></dt>
599
600                   <dd>Will contain the text "true" if the request
601                   currently being processed is a sub-request,
602                   "false" otherwise. Sub-requests may be generated
603                   by modules that need to resolve additional files
604                   or URIs in order to complete their tasks.</dd>
605
606                   <dt><code>REMOTE_ADDR</code></dt>
607
608                   <dd>The IP address of the remote host (see the
609                   <module>mod_remoteip</module> module).</dd>
610
611                   <dt><code>REQUEST_FILENAME</code></dt>
612
613                   <dd>The full local filesystem path to the file or
614                   script matching the request, if this has already
615                   been determined by the server at the time
616                   <code>REQUEST_FILENAME</code> is referenced. Otherwise,
617                   such as when used in virtual host context, the same
618                   value as <code>REQUEST_URI</code>.  Depending on the value of 
619                   <directive module="core">AcceptPathInfo</directive>, the
620                   server may have only used some leading components of the 
621                   <code>REQUEST_URI</code> to map the request to a file.
622                   </dd>
623
624                   <dt><code>REQUEST_SCHEME</code></dt>
625
626                   <dd>Will contain the scheme of the request (usually
627                   "http" or "https"). This value can be influenced with
628                   <directive module="core">ServerName</directive>.</dd>
629
630                   <dt><code>REQUEST_URI</code></dt>
631
632                   <dd>The path component of the requested URI,
633                   such as "/index.html".  This notably excludes the
634                   query string which is available as as its own variable
635                   named <code>QUERY_STRING</code>.</dd>
636
637                   <dt><code>THE_REQUEST</code></dt>
638
639                   <dd>The full HTTP request line sent by the
640                   browser to the server (e.g., "<code>GET
641                   /index.html HTTP/1.1</code>"). This does not
642                   include any additional headers sent by the
643                   browser.  This value has not been unescaped
644                   (decoded), unlike most other variables below.</dd>
645
646                 </dl>
647 </note>
648         </li>
649       </ul>
650
651       <p>If the <em>TestString</em> has the special value <code>expr</code>,
652       the <em>CondPattern</em> will be treated as an
653       <a href="../expr.html">ap_expr</a>. HTTP headers referenced in the
654       expression will be added to the Vary header if the <code>novary</code>
655       flag is not given.</p>
656
657       <p>Other things you should be aware of:</p>
658
659       <ol>
660         <li>
661         <p>The variables SCRIPT_FILENAME and REQUEST_FILENAME
662         contain the same value - the value of the
663         <code>filename</code> field of the internal
664         <code>request_rec</code> structure of the Apache HTTP Server.
665         The first name is the commonly known CGI variable name
666         while the second is the appropriate counterpart of
667         REQUEST_URI (which contains the value of the
668         <code>uri</code> field of <code>request_rec</code>).</p>
669         <p>If a substitution occurred and the rewriting continues,
670         the value of both variables will be updated accordingly.</p>
671         <p>If used in per-server context (<em>i.e.</em>, before the
672         request is mapped to the filesystem) SCRIPT_FILENAME and
673         REQUEST_FILENAME cannot contain the full local filesystem
674         path since the path is unknown at this stage of processing.
675         Both variables will initially contain the value of REQUEST_URI
676         in that case. In order to obtain the full local filesystem
677         path of the request in per-server context, use an URL-based
678         look-ahead <code>%{LA-U:REQUEST_FILENAME}</code> to determine
679         the final value of REQUEST_FILENAME.</p></li>
680
681         <li>
682         <code>%{ENV:variable}</code>, where <em>variable</em> can be
683         any environment variable, is also available.
684         This is looked-up via internal
685         Apache httpd structures and (if not found there) via
686         <code>getenv()</code> from the Apache httpd server process.</li>
687
688         <li>
689         <code>%{SSL:variable}</code>, where <em>variable</em> is the
690         name of an <a href="mod_ssl.html#envvars">SSL environment
691         variable</a>, can be used whether or not
692         <module>mod_ssl</module> is loaded, but will always expand to
693         the empty string if it is not.  Example:
694         <code>%{SSL:SSL_CIPHER_USEKEYSIZE}</code> may expand to
695         <code>128</code>.</li>
696
697         <li>
698         <code>%{HTTP:header}</code>, where <em>header</em> can be
699         any HTTP MIME-header name, can always be used to obtain the
700         value of a header sent in the HTTP request.
701         Example: <code>%{HTTP:Proxy-Connection}</code> is
702         the value of the HTTP header
703         ``<code>Proxy-Connection:</code>''.
704         <p>If a HTTP header is used in a condition this header is added to
705         the Vary header of the response in case the condition evaluates
706         to true for the request. It is <strong>not</strong> added if the
707         condition evaluates to false for the request. Adding the HTTP header
708         to the Vary header of the response is needed for proper caching.</p>
709         <p>It has to be kept in mind that conditions follow a short circuit
710         logic in the case of the '<strong><code>ornext|OR</code></strong>' flag
711         so that certain conditions might not be evaluated at all.</p></li>
712
713         <li>
714         <a id="LA-U" name="LA-U"><code>%{LA-U:variable}</code></a> 
715         can be used for look-aheads which perform
716         an internal (URL-based) sub-request to determine the final
717         value of <em>variable</em>. This can be used to access
718         variable for rewriting which is not available at the current
719         stage, but will be set in a later phase.
720         <p>For instance, to rewrite according to the
721         <code>REMOTE_USER</code> variable from within the
722         per-server context (<code>httpd.conf</code> file) you must
723         use <code>%{LA-U:REMOTE_USER}</code> - this
724         variable is set by the authorization phases, which come
725         <em>after</em> the URL translation phase (during which mod_rewrite
726         operates).</p>
727         <p>On the other hand, because mod_rewrite implements
728         its per-directory context (<code>.htaccess</code> file) via
729         the Fixup phase of the API and because the authorization
730         phases come <em>before</em> this phase, you just can use
731         <code>%{REMOTE_USER}</code> in that context.</p></li>
732
733         <li>
734         <code>%{LA-F:variable}</code> can be used to perform an internal
735         (filename-based) sub-request, to determine the final value
736         of <em>variable</em>. Most of the time, this is the same as
737         LA-U above.</li>
738       </ol>
739
740       <p><em>CondPattern</em> is the condition pattern,
741        a regular expression which is applied to the
742       current instance of the <em>TestString</em>.
743       <em>TestString</em> is first evaluated, before being matched against
744       <em>CondPattern</em>.</p>
745
746       <p><em>CondPattern</em> is usually a
747       <em>perl compatible regular expression</em>, but there is
748       additional syntax available to perform other useful tests against
749       the <em>Teststring</em>:</p>
750
751       <ol>
752         <li>You can prefix the pattern string with a
753         '<code>!</code>' character (exclamation mark) to negate the result
754         of the condition, no matter what kind of <em>CondPattern</em> is used.
755         </li>
756
757         <li>
758           You can perform lexicographical string comparisons:
759
760           <ul>
761             <li>'<strong>&lt;CondPattern</strong>' (lexicographically
762             precedes)<br />
763             Treats the <em>CondPattern</em> as a plain string and
764             compares it lexicographically to <em>TestString</em>. True if
765             <em>TestString</em> lexicographically precedes
766             <em>CondPattern</em>.</li>
767
768             <li>'<strong>&gt;CondPattern</strong>' (lexicographically
769             follows)<br />
770             Treats the <em>CondPattern</em> as a plain string and
771             compares it lexicographically to <em>TestString</em>. True if
772             <em>TestString</em> lexicographically follows
773             <em>CondPattern</em>.</li>
774
775             <li>'<strong>=CondPattern</strong>' (lexicographically
776             equal)<br />
777             Treats the <em>CondPattern</em> as a plain string and
778             compares it lexicographically to <em>TestString</em>. True if
779             <em>TestString</em> is lexicographically equal to
780             <em>CondPattern</em> (the two strings are exactly
781             equal, character for character). If <em>CondPattern</em>
782             is <code>""</code> (two quotation marks) this
783             compares <em>TestString</em> to the empty string.</li>
784
785             <li>'<strong>&lt;=CondPattern</strong>' (lexicographically
786             less than or equal to)<br />
787             Treats the <em>CondPattern</em> as a plain string and
788             compares it lexicographically to <em>TestString</em>. True
789             if <em>TestString</em> lexicographically precedes
790             <em>CondPattern</em>, or is equal to <em>CondPattern</em>
791             (the two strings are equal, character for character).</li>
792
793             <li>'<strong>&gt;=CondPattern</strong>' (lexicographically
794             greater than or equal to)<br />
795             Treats the <em>CondPattern</em> as a plain string and
796             compares it lexicographically to <em>TestString</em>. True
797             if <em>TestString</em> lexicographically follows
798             <em>CondPattern</em>, or is equal to <em>CondPattern</em>
799             (the two strings are equal, character for character).</li>
800         </ul></li>
801
802         <li>
803           You can perform integer comparisons:
804           <ul>
805
806             <li>'<strong>-eq</strong>' (is numerically
807             <strong>eq</strong>ual to)<br />
808             The <em>TestString</em> is treated as an integer, and is
809             numerically compared to the <em>CondPattern</em>. True if
810             the two are numerically equal.</li>
811
812             <li>'<strong>-ge</strong>' (is numerically
813             <strong>g</strong>reater than or <strong>e</strong>qual to)<br />
814             The <em>TestString</em> is treated as an integer, and is
815             numerically compared to the <em>CondPattern</em>. True if
816             the <em>TestString</em> is numerically greater than or equal
817             to the <em>CondPattern</em>.</li>
818
819              <li>'<strong>-gt</strong>' (is numerically
820             <strong>g</strong>reater <strong>t</strong>han)<br />
821             The <em>TestString</em> is treated as an integer, and is
822             numerically compared to the <em>CondPattern</em>. True if
823             the <em>TestString</em> is numerically greater than
824             the <em>CondPattern</em>.</li>
825
826             <li>'<strong>-le</strong>' (is numerically
827             <strong>l</strong>ess than or <strong>e</strong>qual to)<br />
828             The <em>TestString</em> is treated as an integer, and is
829             numerically compared to the <em>CondPattern</em>. True if
830             the <em>TestString</em> is numerically less than or equal
831             to the <em>CondPattern</em>. Avoid confusion with the
832             <strong>-l</strong> by using the <strong>-L</strong> or
833             <strong>-h</strong> variant.</li>
834
835              <li>'<strong>-lt</strong>' (is numerically
836             <strong>l</strong>ess <strong>t</strong>han)<br />
837             The <em>TestString</em> is treated as an integer, and is
838             numerically compared to the <em>CondPattern</em>. True if
839             the <em>TestString</em> is numerically less than
840             the <em>CondPattern</em>. Avoid confusion with the
841             <strong>-l</strong> by using the <strong>-L</strong> or
842             <strong>-h</strong> variant.</li>
843
844            </ul>
845         </li>
846
847         <li>You can perform various file attribute tests:
848           <ul>
849             <li>'<strong>-d</strong>' (is
850             <strong>d</strong>irectory)<br />
851              Treats the <em>TestString</em> as a pathname and tests
852             whether or not it exists, and is a directory.</li>
853
854             <li>'<strong>-f</strong>' (is regular
855             <strong>f</strong>ile)<br />
856              Treats the <em>TestString</em> as a pathname and tests
857             whether or not it exists, and is a regular file.</li>
858
859             <li>'<strong>-F</strong>' (is existing file, via
860             subrequest)<br />
861             Checks whether or not <em>TestString</em> is a valid file,
862             accessible via all the server's currently-configured
863             access controls for that path. This uses an internal
864             subrequest to do the check, so use it with care -
865             it can impact your server's performance!</li>
866
867             <li>'<strong>-H</strong>' (is symbolic link, bash convention)<br />
868             See <strong>-l</strong>.</li>
869
870             <li>'<strong>-l</strong>' (is symbolic
871             <strong>l</strong>ink)<br />
872             Treats the <em>TestString</em> as a pathname and tests
873             whether or not it exists, and is a symbolic link. May also
874             use the bash convention of <strong>-L</strong> or
875             <strong>-h</strong> if there's a possibility of confusion
876             such as when using the <strong>-lt</strong> or
877             <strong>-le</strong> tests.</li>
878
879             <li>'<strong>-L</strong>' (is symbolic link, bash convention)<br />
880             See <strong>-l</strong>.</li>
881
882             <li>'<strong>-s</strong>' (is regular file, with
883             <strong>s</strong>ize)<br />
884             Treats the <em>TestString</em> as a pathname and tests
885             whether or not it exists, and is a regular file with size greater
886             than zero.</li>
887
888             <li><p>'<strong>-U</strong>' (is existing URL, via
889             subrequest)<br />
890             Checks whether or not <em>TestString</em> is a valid URL,
891             accessible via all the server's currently-configured
892             access controls for that path. This uses an internal
893             subrequest to do the check, so use it with care -
894             it can impact your server's performance!</p>
895             <p> This flag <em>only</em> returns information about things
896             like access control, authentication, and authorization.  This flag
897             <em>does not</em> return information about the status code the 
898             configured handler (static file, CGI, proxy, etc.) would have 
899             returned.</p> </li>
900
901             <li>'<strong>-x</strong>' (has e<strong>x</strong>ecutable
902             permissions)<br />
903             Treats the <em>TestString</em> as a pathname and tests
904             whether or not it exists, and has executable permissions.
905             These permissions are determined according to
906             the underlying OS.</li>
907
908           </ul>
909
910         </li>
911
912         <li>
913            <p>If the <em>TestString</em> has the special value <code>expr</code>, the
914            <em>CondPattern</em> will be treated as an
915            <a href="../expr.html">ap_expr</a>.</p>
916
917            <p>
918             In the below example, <code>-strmatch</code> is used to
919             compare the <code>REFERER</code> against the site hostname,
920             to block unwanted hotlinking.
921            </p>
922
923            <highlight language="config">
924            RewriteCond expr "! %{HTTP_REFERER} -strmatch '*://%{HTTP_HOST}/*'"<br />
925            RewriteRule ^/images - [F]
926            </highlight>
927         </li>
928
929         <li>You can also set special flags for
930       <em>CondPattern</em> by appending
931         <strong><code>[</code><em>flags</em><code>]</code></strong>
932       as the third argument to the <code>RewriteCond</code>
933       directive, where <em>flags</em> is a comma-separated list of any of the
934       following flags:
935
936       <ul>
937         <li>'<strong><code>nocase|NC</code></strong>'
938         (<strong>n</strong>o <strong>c</strong>ase)<br />
939         This makes the test case-insensitive - differences
940         between 'A-Z' and 'a-z' are ignored, both in the
941         expanded <em>TestString</em> and the <em>CondPattern</em>.
942         This flag is effective only for comparisons between
943         <em>TestString</em> and <em>CondPattern</em>. It has no
944         effect on filesystem and subrequest checks.</li>
945
946         <li>
947           '<strong><code>ornext|OR</code></strong>'
948           (<strong>or</strong> next condition)<br />
949           Use this to combine rule conditions with a local OR
950           instead of the implicit AND. Typical example:
951
952 <highlight language="config">
953 RewriteCond %{REMOTE_HOST}  ^host1  [OR]
954 RewriteCond %{REMOTE_HOST}  ^host2  [OR]
955 RewriteCond %{REMOTE_HOST}  ^host3
956 RewriteRule ...some special stuff for any of these hosts...
957 </highlight>
958
959           Without this flag you would have to write the condition/rule
960           pair three times.
961         </li>
962
963         <li>'<strong><code>novary|NV</code></strong>'
964         (<strong>n</strong>o <strong>v</strong>ary)<br />
965         If a HTTP header is used in the condition, this flag prevents
966         this header from being added to the Vary header of the response. <br />
967         Using this flag might break proper caching of the response if
968         the representation of this response varies on the value of this header.
969         So this flag should be only used if the meaning of the Vary header
970         is well understood.
971         </li>
972       </ul>
973       </li>
974      </ol>
975
976       <p><strong>Example:</strong></p>
977
978        <p>To rewrite the Homepage of a site according to the
979         ``<code>User-Agent:</code>'' header of the request, you can
980         use the following: </p>
981
982 <highlight language="config">
983 RewriteCond  %{HTTP_USER_AGENT}  (iPhone|Blackberry|Android)
984 RewriteRule  ^/$                 /homepage.mobile.html  [L]
985
986 RewriteRule  ^/$                 /homepage.std.html  [L]
987 </highlight>
988
989         <p>Explanation: If you use a browser which identifies itself
990         as a mobile browser (note that the example is incomplete, as
991         there are many other mobile platforms), the mobile version of
992         the homepage is served. Otherwise, the standard page is served.
993         </p>
994
995 </usage>
996
997 </directivesynopsis>
998
999 <directivesynopsis>
1000 <name>RewriteRule</name>
1001 <description>Defines rules for the rewriting engine</description>
1002 <syntax>RewriteRule
1003       <em>Pattern</em> <em>Substitution</em> [<em>flags</em>]</syntax>
1004 <contextlist><context>server config</context><context>virtual host</context>
1005 <context>directory</context><context>.htaccess</context></contextlist>
1006 <override>FileInfo</override>
1007
1008 <usage>
1009       <p>The <directive>RewriteRule</directive> directive is the real
1010       rewriting workhorse. The directive can occur more than once,
1011       with each instance defining a single rewrite rule. The
1012       order in which these rules are defined is important - this is the order
1013       in which they will be applied at run-time.</p>
1014
1015       <p><a id="patterns" name="patterns"><em>Pattern</em></a> is
1016       a perl compatible <a id="regexp" name="regexp">regular
1017       expression</a>. On the first RewriteRule, it is matched against
1018       the (%-decoded) <a href="directive-dict.html#Syntax">URL-path</a>
1019       of the request, or, in per-directory context (see below), the URL
1020       path relative to that per-directory context. Subsequent patterns
1021       are matched against the output of the last matching RewriteRule.</p>
1022
1023 <note><title><a id="what_is_matched" name="what_is_matched">What is matched?</a></title>
1024       <p>In <directive module="core">VirtualHost</directive> context,
1025       The <em>Pattern</em> will initially be matched against the part of the
1026       URL after the hostname and port, and before the query string (e.g. "/app1/index.html").</p>
1027
1028       <p>In <directive module="core">Directory</directive> and htaccess context,
1029       the <em>Pattern</em> will initially be matched against the
1030       <em>filesystem</em> path, after removing the prefix that led the server
1031       to the current <directive>RewriteRule</directive> (e.g. "app1/index.html"
1032       or "index.html" depending on where the directives are defined).</p>
1033
1034       <p>If you wish to match against the hostname, port, or query string, use a
1035       <directive module="mod_rewrite">RewriteCond</directive> with the
1036       <code>%{HTTP_HOST}</code>, <code>%{SERVER_PORT}</code>, or
1037       <code>%{QUERY_STRING}</code> variables respectively.</p>
1038
1039 </note>
1040
1041 <note><title>Per-directory Rewrites</title>
1042 <ul>
1043 <li>The rewrite engine may be used in <a
1044 href="../howto/htaccess.html">.htaccess</a> files and in <directive type="section"
1045 module="core">Directory</directive> sections, with some additional
1046 complexity.</li>
1047
1048 <li>To enable the rewrite engine in this context, you need to set
1049 "<code>RewriteEngine On</code>" <strong>and</strong>
1050 "<code>Options FollowSymLinks</code>" must be enabled. If your
1051 administrator has disabled override of <code>FollowSymLinks</code> for
1052 a user's directory, then you cannot use the rewrite engine. This
1053 restriction is required for security reasons.</li>
1054
1055 <li>When using the rewrite engine in <code>.htaccess</code> files the
1056 per-directory prefix (which always is the same for a specific
1057 directory) is automatically <em>removed</em> for the RewriteRule pattern matching
1058 and automatically <em>added</em> after any relative (not starting with a
1059 slash or protocol name) substitution encounters the end of a rule set.
1060 See the <directive module="mod_rewrite">RewriteBase</directive>
1061 directive for more information regarding what prefix will be added back to
1062 relative substitutions.</li>
1063
1064 <li> If you wish to match against the full URL-path in a per-directory
1065 (htaccess) RewriteRule, use the <code>%{REQUEST_URI}</code> variable in
1066 a <directive>RewriteCond</directive>.</li>
1067
1068 <li>The removed prefix always ends with a slash, meaning the matching occurs against a string which
1069 <em>never</em> has a leading slash.  Therefore, a <em>Pattern</em> with <code>^/</code> never
1070 matches in per-directory context.</li>
1071
1072 <li>Although rewrite rules are syntactically permitted in <directive
1073 type="section" module="core">Location</directive> and <directive
1074 type="section" module="core">Files</directive> sections
1075 (including their regular expression counterparts), this
1076 should never be necessary and is unsupported. A likely feature
1077 to break in these contexts is relative substitutions.</li>
1078 </ul>
1079 </note>
1080
1081       <p>For some hints on <glossary ref="regex">regular
1082       expressions</glossary>, see
1083       the <a href="../rewrite/intro.html#regex">mod_rewrite
1084       Introduction</a>.</p>
1085
1086       <p>In mod_rewrite, the NOT character
1087       ('<code>!</code>') is also available as a possible pattern
1088       prefix. This enables you to negate a pattern; to say, for instance:
1089       ``<em>if the current URL does <strong>NOT</strong> match this
1090       pattern</em>''. This can be used for exceptional cases, where
1091       it is easier to match the negative pattern, or as a last
1092       default rule.</p>
1093
1094 <note><title>Note</title>
1095 When using the NOT character to negate a pattern, you cannot include
1096 grouped wildcard parts in that pattern. This is because, when the
1097 pattern does NOT match (ie, the negation matches), there are no
1098 contents for the groups. Thus, if negated patterns are used, you
1099 cannot use <code>$N</code> in the substitution string!
1100 </note>
1101
1102       <p>The <a id="rhs" name="rhs"><em>Substitution</em></a> of a
1103       rewrite rule is the string that replaces the original URL-path that
1104       was matched by <em>Pattern</em>.  The <em>Substitution</em> may
1105       be a:</p>
1106
1107       <dl>
1108
1109         <dt>file-system path</dt>
1110
1111         <dd>Designates the location on the file-system of the resource
1112         to be delivered to the client.  Substitutions are only
1113         treated as a file-system path when the rule is configured in 
1114         server (virtualhost) context and the first component of the
1115         path in the substitution exists in the file-system</dd>
1116
1117         <dt>URL-path</dt>
1118
1119         <dd>A <directive
1120         module="core">DocumentRoot</directive>-relative path to the
1121         resource to be served. Note that <module>mod_rewrite</module>
1122         tries to guess whether you have specified a file-system path
1123         or a URL-path by checking to see if the first segment of the
1124         path exists at the root of the file-system. For example, if
1125         you specify a <em>Substitution</em> string of
1126         <code>/www/file.html</code>, then this will be treated as a
1127         URL-path <em>unless</em> a directory named <code>www</code>
1128         exists at the root or your file-system (or, in the case of 
1129         using rewrites in a <code>.htaccess</code> file, relative to
1130         your document root), in which case it will
1131         be treated as a file-system path. If you wish other
1132         URL-mapping directives (such as <directive
1133         module="mod_alias">Alias</directive>) to be applied to the
1134         resulting URL-path, use the <code>[PT]</code> flag as
1135         described below.</dd>
1136
1137         <dt>Absolute URL</dt>
1138
1139         <dd>If an absolute URL is specified,
1140         <module>mod_rewrite</module> checks to see whether the
1141         hostname matches the current host. If it does, the scheme and
1142         hostname are stripped out and the resulting path is treated as
1143         a URL-path. Otherwise, an external redirect is performed for
1144         the given URL. To force an external redirect back to the
1145         current host, see the <code>[R]</code> flag below.</dd>
1146
1147         <dt><code>-</code> (dash)</dt>
1148
1149         <dd>A dash indicates that no substitution should be performed
1150         (the existing path is passed through untouched). This is used
1151         when a flag (see below) needs to be applied without changing
1152         the path.</dd>
1153
1154       </dl>
1155
1156       <p>In addition to plain text, the <em>Substitution</em> string can include</p>
1157
1158       <ol>
1159         <li>back-references (<code>$N</code>) to the RewriteRule
1160         pattern</li>
1161
1162         <li>back-references (<code>%N</code>) to the last matched
1163         RewriteCond pattern</li>
1164
1165         <li>server-variables as in rule condition test-strings
1166         (<code>%{VARNAME}</code>)</li>
1167
1168         <li><a href="#mapfunc">mapping-function</a> calls
1169         (<code>${mapname:key|default}</code>)</li>
1170       </ol>
1171
1172       <p>Back-references are identifiers of the form
1173       <code>$</code><strong>N</strong>
1174       (<strong>N</strong>=0..9), which will be replaced
1175       by the contents of the <strong>N</strong>th group of the
1176       matched <em>Pattern</em>. The server-variables are the same
1177       as for the <em>TestString</em> of a <code>RewriteCond</code>
1178       directive. The mapping-functions come from the
1179       <code>RewriteMap</code> directive and are explained there.
1180       These three types of variables are expanded in the order above.</p>
1181
1182       <p>Rewrite rules are applied to the results of previous rewrite
1183       rules, in the order in which they are defined
1184       in the config file. The URL-path or file-system path (see <a
1185       href="#what_is_matched">"What is matched?"</a>, above) is <strong>completely
1186       replaced</strong> by the <em>Substitution</em> and the
1187       rewriting process continues until all rules have been applied,
1188       or it is explicitly terminated by an
1189       <a href="../rewrite/flags.html#flag_l"><code><strong>L</strong></code> flag</a>,
1190       or other flag which implies immediate termination, such as
1191       <code><strong>END</strong></code> or
1192       <code><strong>F</strong></code>.</p>
1193
1194      <note><title>Modifying the Query String</title>
1195       <p>By default, the query string is passed through unchanged. You
1196       can, however, create URLs in the substitution string containing
1197       a query string part. Simply use a question mark inside the
1198       substitution string to indicate that the following text should
1199       be re-injected into the query string. When you want to erase an
1200       existing query string, end the substitution string with just a
1201       question mark. To combine new and old query strings, use the
1202       <code>[QSA]</code> flag.</p>
1203      </note>
1204
1205       <p>Additionally you can set special <a name="rewriteflags"
1206       id="rewriteflags">actions</a> to be performed by
1207       appending <strong><code>[</code><em>flags</em><code>]</code></strong>
1208       as the third argument to the <code>RewriteRule</code>
1209       directive. <em>Flags</em> is a comma-separated list, surround by square
1210       brackets, of any of the flags in the following table. More
1211       details, and examples, for each flag, are available in the <a
1212       href="../rewrite/flags.html">Rewrite Flags document</a>.</p>
1213
1214     <table border="1" style="zebra">
1215     <tr><th>Flag and syntax</th>
1216         <th>Function</th>
1217     </tr>
1218     <tr>
1219         <td>B</td>
1220         <td>Escape non-alphanumeric characters in backreferences <em>before</em> 
1221         applying the transformation. <em><a
1222         href="../rewrite/flags.html#flag_b">details ...</a></em></td>
1223     </tr>
1224     <tr>
1225         <td>backrefnoplus|BNP</td>
1226         <td>If backreferences are being escaped, spaces should be escaped to 
1227         %20 instead of +. Useful when the backreference will be used in the
1228         path component rather than the query string.<em><a
1229         href="../rewrite/flags.html#flag_bnp">details ...</a></em></td>
1230     </tr>
1231
1232     <tr>
1233         <td>chain|C</td>
1234         <td>Rule is chained to the following rule. If the rule fails,
1235         the rule(s) chained to it will be skipped. <em><a
1236         href="../rewrite/flags.html#flag_c">details ...</a></em></td>
1237     </tr>
1238     <tr>
1239         <td>cookie|CO=<em>NAME</em>:<em>VAL</em></td>
1240         <td>Sets a cookie in the client browser. Full syntax is:
1241         CO=<em>NAME</em>:<em>VAL</em>:<em>domain</em>[:<em>lifetime</em>[:<em>path</em>[:<em>secure</em>[:<em>httponly</em>]]]] <em><a href="../rewrite/flags.html#flag_co">details ...</a></em>
1242         </td>
1243     </tr>
1244     <tr>
1245         <td>discardpath|DPI</td>
1246         <td>Causes the PATH_INFO portion of the rewritten URI to be
1247         discarded. <em><a href="../rewrite/flags.html#flag_dpi">details
1248         ...</a></em></td>
1249     </tr>
1250     <tr>
1251         <td>END</td>
1252         <td>Stop the rewriting process immediately and don't apply any
1253         more rules. Also prevents further execution of rewrite rules
1254         in per-directory and .htaccess context. (Available in 2.3.9 and later)
1255         <em><a href="../rewrite/flags.html#flag_end">details ...</a></em></td>
1256     </tr>
1257     <tr>
1258         <td>env|E=[!]<em>VAR</em>[:<em>VAL</em>]</td>
1259         <td>Causes an environment variable <em>VAR</em> to be set (to the
1260         value <em>VAL</em> if provided). The form !<em>VAR</em> causes
1261         the environment variable <em>VAR</em> to be unset.
1262         <em><a href="../rewrite/flags.html#flag_e">details ...</a></em></td>
1263     </tr>
1264     <tr>
1265         <td>forbidden|F</td>
1266         <td>Returns a 403 FORBIDDEN response to the client browser.
1267         <em><a href="../rewrite/flags.html#flag_f">details ...</a></em></td>
1268     </tr>
1269     <tr>
1270         <td>gone|G</td>
1271         <td>Returns a 410 GONE response to the client browser. <em><a
1272         href="../rewrite/flags.html#flag_g">details ...</a></em></td>
1273     </tr>
1274     <tr>
1275         <td>Handler|H=<em>Content-handler</em></td>
1276         <td>Causes the resulting URI to be sent to the specified
1277         <em>Content-handler</em> for processing. <em><a
1278         href="../rewrite/flags.html#flag_h">details ...</a></em></td>
1279     </tr>
1280     <tr>
1281         <td>last|L</td>
1282         <td>Stop the rewriting process immediately and don't apply any
1283         more rules. Especially note caveats for per-directory and
1284         .htaccess context (see also the END flag). <em><a
1285         href="../rewrite/flags.html#flag_l">details ...</a></em></td>
1286     </tr>
1287     <tr>
1288         <td>next|N</td>
1289         <td>Re-run the rewriting process, starting again with the first
1290         rule, using the result of the ruleset so far as a starting
1291         point. <em><a href="../rewrite/flags.html#flag_n">details
1292         ...</a></em></td>
1293     </tr>
1294     <tr>
1295         <td>nocase|NC</td>
1296         <td>Makes the pattern comparison case-insensitive.
1297         <em><a href="../rewrite/flags.html#flag_nc">details ...</a></em></td>
1298     </tr>
1299     <tr>
1300         <td>noescape|NE</td>
1301         <td>Prevent mod_rewrite from applying hexcode escaping of
1302         special characters in the result of the rewrite. <em><a
1303         href="../rewrite/flags.html#flag_ne">details ...</a></em></td>
1304     </tr>
1305     <tr>
1306         <td>nosubreq|NS</td>
1307         <td>Causes a rule to be skipped if the current request is an
1308         internal sub-request. <em><a
1309         href="../rewrite/flags.html#flag_ns">details ...</a></em></td>
1310     </tr>
1311     <tr>
1312         <td>proxy|P</td>
1313         <td>Force the substitution URL to be internally sent as a proxy
1314         request. <em><a href="../rewrite/flags.html#flag_p">details
1315         ...</a></em></td>
1316     </tr>
1317     <tr>
1318         <td>passthrough|PT</td>
1319         <td>Forces the resulting URI to be passed back to the URL
1320         mapping engine for processing of other URI-to-filename
1321         translators, such as <code>Alias</code> or
1322         <code>Redirect</code>. <em><a
1323         href="../rewrite/flags.html#flag_pt">details ...</a></em></td>
1324     </tr>
1325     <tr>
1326         <td>qsappend|QSA</td>
1327         <td>Appends any query string from the original request URL to 
1328         any query string created in the rewrite target.<em><a
1329         href="../rewrite/flags.html#flag_qsa">details ...</a></em></td>
1330     </tr>
1331     <tr>
1332         <td>qsdiscard|QSD</td>
1333         <td>Discard any query string attached to the incoming URI.
1334         <em><a href="../rewrite/flags.html#flag_qsd">details
1335         ...</a></em></td>
1336     </tr>
1337     <tr>
1338         <td>redirect|R[=<em>code</em>]</td>
1339         <td>Forces an external redirect, optionally with the specified
1340         HTTP status code. <em><a
1341         href="../rewrite/flags.html#flag_r">details ...</a></em>
1342         </td>
1343     </tr>
1344     <tr>
1345         <td>skip|S=<em>num</em></td>
1346         <td>Tells the rewriting engine to skip the next <em>num</em>
1347         rules if the current rule matches. <em><a
1348         href="../rewrite/flags.html#flag_s">details ...</a></em></td>
1349     </tr>
1350     <tr>
1351         <td>type|T=<em>MIME-type</em></td>
1352         <td>Force the <glossary>MIME-type</glossary> of the target file
1353         to be the specified type. <em><a
1354         href="../rewrite/flags.html#flag_t">details ...</a></em></td>
1355     </tr>
1356     </table>
1357
1358 <note><title>Home directory expansion</title>
1359 <p> When the substitution string begins with a string
1360 resembling "/~user" (via explicit text or backreferences), mod_rewrite performs
1361 home directory expansion independent of the presence or configuration
1362 of <module>mod_userdir</module>.</p>
1363
1364 <p> This expansion does not occur when the <em>PT</em>
1365 flag is used on the <directive module="mod_rewrite">RewriteRule</directive>
1366 directive.</p>
1367 </note>
1368
1369
1370      <p>Here are all possible substitution combinations and their
1371       meanings:</p>
1372
1373       <p><strong>Inside per-server configuration
1374       (<code>httpd.conf</code>)<br />
1375        for request ``<code>GET
1376       /somepath/pathinfo</code>'':</strong><br />
1377       </p>
1378
1379 <table border="1" style="zebra">
1380 <tr>
1381 <th>Given Rule</th>
1382 <th>Resulting Substitution</th>
1383 </tr>
1384
1385 <tr>
1386 <td>^/somepath(.*) otherpath$1</td>
1387 <td>invalid, not supported</td>
1388 </tr>
1389
1390 <tr>
1391 <td>^/somepath(.*) otherpath$1  [R]</td>
1392 <td>invalid, not supported</td>
1393 </tr>
1394
1395 <tr>
1396 <td>^/somepath(.*) otherpath$1  [P]</td>
1397 <td>invalid, not supported</td>
1398 </tr>
1399
1400 <tr>
1401 <td>^/somepath(.*) /otherpath$1</td>
1402 <td>/otherpath/pathinfo</td>
1403 </tr>
1404
1405 <tr>
1406 <td>^/somepath(.*) /otherpath$1 [R]</td>
1407 <td>http://thishost/otherpath/pathinfo via external redirection</td>
1408 </tr>
1409
1410 <tr>
1411 <td>^/somepath(.*) /otherpath$1 [P]</td>
1412 <td>doesn't make sense, not supported</td>
1413 </tr>
1414
1415 <tr>
1416 <td>^/somepath(.*) http://thishost/otherpath$1</td>
1417 <td>/otherpath/pathinfo</td>
1418 </tr>
1419
1420 <tr>
1421 <td>^/somepath(.*) http://thishost/otherpath$1 [R]</td>
1422 <td>http://thishost/otherpath/pathinfo via external redirection</td>
1423 </tr>
1424
1425 <tr>
1426 <td>^/somepath(.*) http://thishost/otherpath$1 [P]</td>
1427 <td>doesn't make sense, not supported</td>
1428 </tr>
1429
1430 <tr>
1431 <td>^/somepath(.*) http://otherhost/otherpath$1</td>
1432 <td>http://otherhost/otherpath/pathinfo via external redirection</td>
1433 </tr>
1434
1435 <tr>
1436 <td>^/somepath(.*) http://otherhost/otherpath$1 [R]</td>
1437 <td>http://otherhost/otherpath/pathinfo via external redirection (the [R] flag is redundant)</td>
1438 </tr>
1439
1440 <tr>
1441 <td>^/somepath(.*) http://otherhost/otherpath$1 [P]</td>
1442 <td>http://otherhost/otherpath/pathinfo via internal proxy</td>
1443 </tr>
1444 </table>
1445
1446       <p><strong>Inside per-directory configuration for
1447       <code>/somepath</code><br />
1448        (<code>/physical/path/to/somepath/.htaccess</code>, with
1449       <code>RewriteBase /somepath</code>)<br />
1450        for request ``<code>GET
1451       /somepath/localpath/pathinfo</code>'':</strong><br />
1452      </p>
1453
1454 <table border="1" style="zebra">
1455
1456 <tr>
1457 <th>Given Rule</th>
1458 <th>Resulting Substitution</th>
1459 </tr>
1460
1461 <tr>
1462 <td>^localpath(.*) otherpath$1</td>
1463 <td>/somepath/otherpath/pathinfo</td>
1464 </tr>
1465
1466 <tr>
1467 <td>^localpath(.*) otherpath$1  [R]</td>
1468 <td>http://thishost/somepath/otherpath/pathinfo via external
1469 redirection</td>
1470 </tr>
1471
1472 <tr>
1473 <td>^localpath(.*) otherpath$1  [P]</td>
1474 <td>doesn't make sense, not supported</td>
1475 </tr>
1476
1477 <tr>
1478 <td>^localpath(.*) /otherpath$1</td>
1479 <td>/otherpath/pathinfo</td>
1480 </tr>
1481
1482 <tr>
1483 <td>^localpath(.*) /otherpath$1 [R]</td>
1484 <td>http://thishost/otherpath/pathinfo via external redirection</td>
1485 </tr>
1486
1487 <tr>
1488 <td>^localpath(.*) /otherpath$1 [P]</td>
1489 <td>doesn't make sense, not supported</td>
1490 </tr>
1491
1492 <tr>
1493 <td>^localpath(.*) http://thishost/otherpath$1</td>
1494 <td>/otherpath/pathinfo</td>
1495 </tr>
1496
1497 <tr>
1498 <td>^localpath(.*) http://thishost/otherpath$1 [R]</td>
1499 <td>http://thishost/otherpath/pathinfo via external redirection</td>
1500 </tr>
1501
1502 <tr>
1503 <td>^localpath(.*) http://thishost/otherpath$1 [P]</td>
1504 <td>doesn't make sense, not supported</td>
1505 </tr>
1506
1507 <tr>
1508 <td>^localpath(.*) http://otherhost/otherpath$1</td>
1509 <td>http://otherhost/otherpath/pathinfo via external redirection</td>
1510 </tr>
1511
1512 <tr>
1513 <td>^localpath(.*) http://otherhost/otherpath$1 [R]</td>
1514 <td>http://otherhost/otherpath/pathinfo via external redirection (the [R] flag is redundant)</td>
1515 </tr>
1516
1517 <tr>
1518 <td>^localpath(.*) http://otherhost/otherpath$1 [P]</td>
1519 <td>http://otherhost/otherpath/pathinfo via internal proxy</td>
1520 </tr>
1521
1522 </table>
1523
1524   </usage>
1525  </directivesynopsis>
1526 </modulesynopsis>