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