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