]> granicus.if.org Git - apache/blob - docs/manual/rewrite/remapping.html.en
Enclose parameters in quotation marks for <{Files,Directory,Location}{,Match}>
[apache] / docs / manual / rewrite / remapping.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>Redirecting and Remapping with mod_rewrite - Apache HTTP Server Version 2.5</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.min.js" type="text/javascript">
13 </script>
14
15 <link href="../images/favicon.ico" rel="shortcut icon" /></head>
16 <body id="manual-page"><div id="page-header">
17 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
18 <p class="apache">Apache HTTP Server Version 2.5</p>
19 <img alt="" src="../images/feather.gif" /></div>
20 <div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="../images/left.gif" /></a></div>
21 <div id="path">
22 <a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="../">Version 2.5</a> &gt; <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>Redirecting and Remapping with mod_rewrite</h1>
23 <div class="toplang">
24 <p><span>Available Languages: </span><a href="../en/rewrite/remapping.html" title="English">&nbsp;en&nbsp;</a></p>
25 </div>
26
27
28 <p>This document supplements the <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
29 <a href="../mod/mod_rewrite.html">reference documentation</a>. It describes
30 how you can use <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> to redirect and remap
31 request. This includes many examples of common uses of mod_rewrite,
32 including detailed descriptions of how each works.</p>
33
34 <div class="warning">Note that many of these examples won't work unchanged in your
35 particular server configuration, so it's important that you understand
36 them, rather than merely cutting and pasting the examples into your
37 configuration.</div>
38
39 </div>
40 <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#old-to-new">From Old to New (internal)</a></li>
41 <li><img alt="" src="../images/down.gif" /> <a href="#old-to-new-extern">Rewriting From Old to New (external)</a></li>
42 <li><img alt="" src="../images/down.gif" /> <a href="#movehomedirs">Resource Moved to Another Server</a></li>
43 <li><img alt="" src="../images/down.gif" /> <a href="#static-to-dynamic">From Static to Dynamic</a></li>
44 <li><img alt="" src="../images/down.gif" /> <a href="#backward-compatibility">Backward Compatibility for file extension change</a></li>
45 <li><img alt="" src="../images/down.gif" /> <a href="#canonicalhost">Canonical Hostnames</a></li>
46 <li><img alt="" src="../images/down.gif" /> <a href="#multipledirs">Search for pages in more than one directory</a></li>
47 <li><img alt="" src="../images/down.gif" /> <a href="#archive-access-multiplexer">Redirecting to Geographically Distributed Servers</a></li>
48 <li><img alt="" src="../images/down.gif" /> <a href="#canonicalurl">Canonical URLs</a></li>
49 <li><img alt="" src="../images/down.gif" /> <a href="#moveddocroot">Moved <code>DocumentRoot</code></a></li>
50 <li><img alt="" src="../images/down.gif" /> <a href="#fallback-resource">Fallback Resource</a></li>
51 <li><img alt="" src="../images/down.gif" /> <a href="#rewrite-query">Rewrite query string</a></li>
52 </ul><h3>See also</h3><ul class="seealso"><li><a href="../mod/mod_rewrite.html">Module documentation</a></li><li><a href="intro.html">mod_rewrite introduction</a></li><li><a href="access.html">Controlling access</a></li><li><a href="vhosts.html">Virtual hosts</a></li><li><a href="proxy.html">Proxying</a></li><li><a href="rewritemap.html">Using RewriteMap</a></li><li><a href="advanced.html">Advanced techniques</a></li><li><a href="avoid.html">When not to use mod_rewrite</a></li></ul><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div>
53 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
54 <div class="section">
55 <h2><a name="old-to-new" id="old-to-new">From Old to New (internal)</a></h2>
56
57   
58
59   <dl>
60     <dt>Description:</dt>
61
62     <dd>
63       <p>Assume we have recently renamed the page
64       <code>foo.html</code> to <code>bar.html</code> and now want
65       to provide the old URL for backward compatibility. However,
66       we want that users of the old URL even not recognize that
67       the pages was renamed - that is, we don't want the address to
68       change in their browser.</p>
69     </dd>
70
71     <dt>Solution:</dt>
72
73     <dd>
74       <p>We rewrite the old URL to the new one internally via the
75       following rule:</p>
76
77 <pre class="prettyprint lang-config">RewriteEngine  on
78 RewriteRule    ^<strong>/foo</strong>\.html$  <strong>/bar</strong>.html [PT]</pre>
79
80     </dd>
81   </dl>
82
83 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
84 <div class="section">
85 <h2><a name="old-to-new-extern" id="old-to-new-extern">Rewriting From Old to New (external)</a></h2>
86
87   
88
89   <dl>
90     <dt>Description:</dt>
91
92     <dd>
93       <p>Assume again that we have recently renamed the page
94       <code>foo.html</code> to <code>bar.html</code> and now want
95       to provide the old URL for backward compatibility. But this
96       time we want that the users of the old URL get hinted to
97       the new one, i.e. their browsers Location field should
98       change, too.</p>
99     </dd>
100
101     <dt>Solution:</dt>
102
103     <dd>
104       <p>We force a HTTP redirect to the new URL which leads to a
105       change of the browsers and thus the users view:</p>
106
107 <pre class="prettyprint lang-config">RewriteEngine  on
108 RewriteRule    ^<strong>/foo</strong>\.html$  <strong>bar</strong>.html  [<strong>R</strong>]</pre>
109
110 </dd>
111
112 <dt>Discussion</dt>
113
114     <dd>
115     <p>In this example, as contrasted to the <a href="#old-to-new-intern">internal</a> example above, we can simply
116     use the Redirect directive. mod_rewrite was used in that earlier
117     example in order to hide the redirect from the client:</p>
118
119     <pre class="prettyprint lang-config">Redirect /foo.html /bar.html</pre>
120
121
122     </dd>
123   </dl>
124
125 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
126 <div class="section">
127 <h2><a name="movehomedirs" id="movehomedirs">Resource Moved to Another Server</a></h2>
128
129   
130
131   <dl>
132     <dt>Description:</dt>
133
134     <dd>
135       <p>If a resource has moved to another server, you may wish to have
136       URLs continue to work for a time on the old server while people
137       update their bookmarks.</p>
138     </dd>
139
140     <dt>Solution:</dt>
141
142     <dd>
143       <p>You can use <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> to redirect these URLs
144       to the new server, but you might also consider using the Redirect
145       or RedirectMatch directive.</p>
146
147 <pre class="prettyprint lang-config">#With mod_rewrite
148 RewriteEngine on
149 RewriteRule   ^/docs/(.+)  http://new.example.com/docs/$1  [R,L]</pre>
150
151
152 <pre class="prettyprint lang-config">#With RedirectMatch
153 RedirectMatch ^/docs/(.*) http://new.example.com/docs/$1</pre>
154
155
156 <pre class="prettyprint lang-config">#With Redirect
157 Redirect /docs/ http://new.example.com/docs/</pre>
158
159     </dd>
160   </dl>
161
162 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
163 <div class="section">
164 <h2><a name="static-to-dynamic" id="static-to-dynamic">From Static to Dynamic</a></h2>
165
166   
167
168   <dl>
169     <dt>Description:</dt>
170
171     <dd>
172       <p>How can we transform a static page
173       <code>foo.html</code> into a dynamic variant
174       <code>foo.cgi</code> in a seamless way, i.e. without notice
175       by the browser/user.</p>
176     </dd>
177
178     <dt>Solution:</dt>
179
180     <dd>
181       <p>We just rewrite the URL to the CGI-script and force the
182       handler to be <strong>cgi-script</strong> so that it is
183       executed as a CGI program.
184       This way a request to <code>/~quux/foo.html</code>
185       internally leads to the invocation of
186       <code>/~quux/foo.cgi</code>.</p>
187
188 <pre class="prettyprint lang-config">RewriteEngine  on
189 RewriteBase    /~quux/
190 RewriteRule    ^foo\.html$  foo.cgi   [H=<strong>cgi-script</strong>]</pre>
191
192     </dd>
193   </dl>
194
195 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
196 <div class="section">
197 <h2><a name="backward-compatibility" id="backward-compatibility">Backward Compatibility for file extension change</a></h2>
198
199   
200
201   <dl>
202     <dt>Description:</dt>
203
204     <dd>
205       <p>How can we make URLs backward compatible (still
206       existing virtually) after migrating <code>document.YYYY</code>
207       to <code>document.XXXX</code>, e.g. after translating a
208       bunch of <code>.html</code> files to <code>.php</code>?</p>
209     </dd>
210
211     <dt>Solution:</dt>
212
213     <dd>
214       <p>We rewrite the name to its basename and test for
215       existence of the new extension. If it exists, we take
216       that name, else we rewrite the URL to its original state.</p>
217
218 <pre class="prettyprint lang-config">#   backward compatibility ruleset for
219 #   rewriting document.html to document.php
220 #   when and only when document.php exists
221 &lt;Directory "/var/www/htdocs"&gt;
222     RewriteEngine on
223     RewriteBase /var/www/htdocs
224
225     RewriteCond $1.php -f
226     RewriteCond $1.html !-f
227     RewriteRule ^(.*).html$ $1.php
228 &lt;/Directory&gt;</pre>
229
230     </dd>
231
232     <dt>Discussion</dt>
233     <dd>
234     <p>This example uses an often-overlooked feature of mod_rewrite,
235     by taking advantage of the order of execution of the ruleset. In
236     particular, mod_rewrite evaluates the left-hand-side of the
237     RewriteRule before it evaluates the RewriteCond directives.
238     Consequently, $1 is already defined by the time the RewriteCond
239     directives are evaluated. This allows us to test for the existence
240     of the original (<code>document.html</code>) and target
241     (<code>document.php</code>) files using the same base filename.</p>
242
243     <p>This ruleset is designed to use in a per-directory context (In a
244     &lt;Directory&gt; block or in a .htaccess file), so that the
245     <code>-f</code> checks are looking at the correct directory path.
246     You may need to set a <code class="directive"><a href="../mod/mod_rewrite.html#rewritebase">RewriteBase</a></code> directive to specify the
247     directory base that you're working in.</p>
248     </dd>
249   </dl>
250
251 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
252 <div class="section">
253 <h2><a name="canonicalhost" id="canonicalhost">Canonical Hostnames</a></h2>
254
255
256
257       <dl>
258         <dt>Description:</dt>
259
260         <dd>The goal of this rule is to force the use of a particular
261         hostname, in preference to other hostnames which may be used to
262         reach the same site. For example, if you wish to force the use
263         of <strong>www.example.com</strong> instead of
264         <strong>example.com</strong>, you might use a variant of the
265         following recipe.</dd>
266
267         <dt>Solution:</dt>
268
269         <dd>
270
271 <p>The very best way to solve this doesn't involve mod_rewrite at all,
272 but rather uses the <code class="directive"><a href="../mod/mod_alias.html#redirect">Redirect</a></code>
273 directive placed in a virtual host for the non-canonical
274 hostname(s).</p>
275
276 <pre class="prettyprint lang-config">&lt;VirtualHost *:80&gt;
277   ServerName undesired.example.com
278   ServerAlias example.com notthis.example.com
279
280   Redirect / http://www.example.com/
281 &lt;/VirtualHost&gt;
282
283 &lt;VirtualHost *:80&gt;
284   ServerName www.example.com
285 &lt;/VirtualHost&gt;</pre>
286
287
288 <p>You can alternatively accomplish this using the
289 <code class="directive"><a href="../mod/core.html#if">&lt;If&gt;</a></code>
290 directive:</p>
291
292 <pre class="prettyprint lang-config">&lt;If "%{HTTP_HOST} != 'www.example.com'"&gt;
293         Redirect / http://www.example.com/
294 &lt;/If&gt;</pre>
295
296
297 <p>Or, for example, to redirect a portion of your site to HTTPS, you
298 might do the following:</p>
299
300 <pre class="prettyprint lang-config">&lt;If "%{SERVER_PROTOCOL} != 'HTTPS'"&gt;
301         Redirect /admin/ https://www.example.com/admin/
302 &lt;/If&gt;</pre>
303
304
305 <p>If, for whatever reason, you still want to use <code>mod_rewrite</code>
306 - if, for example, you need this to work with a larger set of RewriteRules -
307 you might use one of the recipes below.</p>
308
309 <p>For sites running on a port other than 80:</p>
310 <pre class="prettyprint lang-config">RewriteCond %{HTTP_HOST}   !^www\.example\.com [NC]
311 RewriteCond %{HTTP_HOST}   !^$
312 RewriteCond %{SERVER_PORT} !^80$
313 RewriteRule ^/?(.*)         http://www.example.com:%{SERVER_PORT}/$1 [L,R,NE]</pre>
314
315
316 <p>And for a site running on port 80</p>
317 <pre class="prettyprint lang-config">RewriteCond %{HTTP_HOST}   !^www\.example\.com [NC]
318 RewriteCond %{HTTP_HOST}   !^$
319 RewriteRule ^/?(.*)         http://www.example.com/$1 [L,R,NE]</pre>
320
321
322         <p>
323         If you wanted to do this generically for all domain names - that
324         is, if you want to redirect <strong>example.com</strong> to
325         <strong>www.example.com</strong> for all possible values of
326         <strong>example.com</strong>, you could use the following
327         recipe:</p>
328
329 <pre class="prettyprint lang-config">RewriteCond %{HTTP_HOST} !^www\. [NC]
330 RewriteCond %{HTTP_HOST} !^$
331 RewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [L,R,NE]</pre>
332
333
334     <p>These rulesets will work either in your main server configuration
335     file, or in a <code>.htaccess</code> file placed in the <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code> of the server.</p>
336         </dd>
337       </dl>
338
339 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
340 <div class="section">
341 <h2><a name="multipledirs" id="multipledirs">Search for pages in more than one directory</a></h2>
342
343   
344
345   <dl>
346     <dt>Description:</dt>
347
348     <dd>
349       <p>A particular resource might exist in one of several places, and
350       we want to look in those places for the resource when it is
351       requested. Perhaps we've recently rearranged our directory
352       structure, dividing content into several locations.</p>
353     </dd>
354
355     <dt>Solution:</dt>
356
357     <dd>
358       <p>The following ruleset searches in two directories to find the
359       resource, and, if not finding it in either place, will attempt to
360       just serve it out of the location requested.</p>
361
362 <pre class="prettyprint lang-config">RewriteEngine on
363
364 #   first try to find it in dir1/...
365 #   ...and if found stop and be happy:
366 RewriteCond         %{DOCUMENT_ROOT}/<strong>dir1</strong>/%{REQUEST_URI}  -f
367 RewriteRule  ^(.+)  %{DOCUMENT_ROOT}/<strong>dir1</strong>/$1  [L]
368
369 #   second try to find it in dir2/...
370 #   ...and if found stop and be happy:
371 RewriteCond         %{DOCUMENT_ROOT}/<strong>dir2</strong>/%{REQUEST_URI}  -f
372 RewriteRule  ^(.+)  %{DOCUMENT_ROOT}/<strong>dir2</strong>/$1  [L]
373
374 #   else go on for other Alias or ScriptAlias directives,
375 #   etc.
376 RewriteRule   ^  -  [PT]</pre>
377
378     </dd>
379   </dl>
380
381 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
382 <div class="section">
383 <h2><a name="archive-access-multiplexer" id="archive-access-multiplexer">Redirecting to Geographically Distributed Servers</a></h2>
384
385   
386
387   <dl>
388     <dt>Description:</dt>
389
390     <dd>
391     <p>We have numerous mirrors of our website, and want to redirect
392     people to the one that is located in the country where they are
393     located.</p>
394     </dd>
395
396     <dt>Solution:</dt>
397
398     <dd>
399     <p>Looking at the hostname of the requesting client, we determine
400     which country they are coming from. If we can't do a lookup on their
401     IP address, we fall back to a default server.</p>
402     <p>We'll use a <code class="directive"><a href="../mod/mod_rewrite.html#rewritemap">RewriteMap</a></code>
403     directive to build a list of servers that we wish to use.</p>
404
405 <pre class="prettyprint lang-config">HostnameLookups on
406 RewriteEngine on
407 RewriteMap    multiplex         txt:/path/to/map.mirrors
408 RewriteCond  %{REMOTE_HOST}     ([a-z]+)$ [NC]
409 RewriteRule   ^/(.*)$  ${multiplex:<strong>%1</strong>|http://www.example.com/}$1  [R,L]</pre>
410
411
412 <div class="example"><p><code>
413 ##  map.mirrors -- Multiplexing Map<br />
414 <br />
415 de        http://www.example.de/<br />
416 uk        http://www.example.uk/<br />
417 com       http://www.example.com/<br />
418 ##EOF##
419 </code></p></div>
420     </dd>
421
422     <dt>Discussion</dt>
423     <dd>
424     <div class="warning">This ruleset relies on
425     <code class="directive"><a href="../mod/core.html#hostnamelookups">HostNameLookups</a></code>
426     being set <code>on</code>, which can be
427     a significant performance hit.</div>
428
429     <p>The <code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code>
430     directive captures the last portion of the hostname of the
431     requesting client - the country code - and the following RewriteRule
432     uses that value to look up the appropriate mirror host in the map
433     file.</p>
434     </dd>
435   </dl>
436
437 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
438 <div class="section">
439 <h2><a name="canonicalurl" id="canonicalurl">Canonical URLs</a></h2>
440
441
442
443 <dl>
444  <dt>Description:</dt>
445
446    <dd>
447      <p>On some webservers there is more than one URL for a
448      resource. Usually there are canonical URLs (which are be
449      actually used and distributed) and those which are just
450      shortcuts, internal ones, and so on. Independent of which URL the
451      user supplied with the request, they should finally see the
452      canonical one in their browser address bar.</p>
453    </dd>
454
455    <dt>Solution:</dt>
456
457      <dd>
458        <p>We do an external HTTP redirect for all non-canonical
459        URLs to fix them in the location view of the Browser and
460        for all subsequent requests. In the example ruleset below
461        we replace <code>/puppies</code> and <code>/canines</code>
462        by the canonical <code>/dogs</code>.</p>
463
464 <pre class="prettyprint lang-config">RewriteRule   ^/(puppies|canines)/(.*)    /dogs/$2  [R]</pre>
465
466         </dd>
467
468      <dt>Discussion:</dt>
469      <dd>
470      This should really be accomplished with Redirect or RedirectMatch
471      directives:
472
473      <pre class="prettyprint lang-config">RedirectMatch ^/(puppies|canines)/(.*) /dogs/$2</pre>
474
475      </dd>
476       </dl>
477
478 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
479 <div class="section">
480 <h2><a name="moveddocroot" id="moveddocroot">Moved <code>DocumentRoot</code></a></h2>
481
482   
483
484   <dl>
485     <dt>Description:</dt>
486
487     <dd>
488 <p>Usually the <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>
489 of the webserver directly relates to the URL "<code>/</code>".
490 But often this data is not really of top-level priority. For example,
491 you may wish for visitors, on first entering a site, to go to a
492 particular subdirectory <code>/about/</code>. This may be accomplished
493 using the following ruleset:</p>
494 </dd>
495
496     <dt>Solution:</dt>
497
498     <dd>
499       <p>We redirect the URL <code>/</code> to
500       <code>/about/</code>:
501       </p>
502
503 <pre class="prettyprint lang-config">RewriteEngine on
504 RewriteRule   ^/$  /about/  [<strong>R</strong>]</pre>
505
506
507 <p>Note that this can also be handled using the <code class="directive"><a href="../mod/mod_alias.html#redirectmatch">RedirectMatch</a></code> directive:</p>
508
509 <pre class="prettyprint lang-config">RedirectMatch ^/$ http://example.com/about/</pre>
510
511
512 <p>Note also that the example rewrites only the root URL. That is, it
513 rewrites a request for <code>http://example.com/</code>, but not a
514 request for <code>http://example.com/page.html</code>. If you have in
515 fact changed your document root - that is, if <strong>all</strong> of
516 your content is in fact in that subdirectory, it is greatly preferable
517 to simply change your <code class="directive"><a href="../mod/core.html#documentroot">DocumentRoot</a></code>
518 directive, or move all of the content up one directory,
519 rather than rewriting URLs.</p>
520 </dd>
521 </dl>
522
523 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
524 <div class="section">
525 <h2><a name="fallback-resource" id="fallback-resource">Fallback Resource</a></h2>
526
527
528 <dl>
529 <dt>Description:</dt>
530 <dd>You want a single resource (say, a certain file, like index.php) to
531 handle all requests that come to a particular directory, except those
532 that should go to an existing resource such as an image, or a css file.</dd>
533
534 <dt>Solution:</dt>
535 <dd>
536 <p>As of version 2.2.16, you should use the <code class="directive"><a href="../mod/mod_dir.html#fallbackresource">FallbackResource</a></code> directive for this:</p>
537
538 <pre class="prettyprint lang-config">&lt;Directory "/var/www/my_blog"&gt;
539   FallbackResource index.php
540 &lt;/Directory&gt;</pre>
541
542
543 <p>However, in earlier versions of Apache, or if your needs are more
544 complicated than this, you can use a variation of the following rewrite
545 set to accomplish the same thing:</p>
546
547 <pre class="prettyprint lang-config">&lt;Directory "/var/www/my_blog"&gt;
548   RewriteBase /my_blog
549
550   RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-f
551   RewriteCond /var/www/my_blog/%{REQUEST_FILENAME} !-d
552   RewriteRule ^ index.php [PT]
553 &lt;/Directory&gt;</pre>
554
555
556 <p>If, on the other hand, you wish to pass the requested URI as a query
557 string argument to index.php, you can replace that RewriteRule with:</p>
558
559 <pre class="prettyprint lang-config">RewriteRule (.*) index.php?$1 [PT,QSA]</pre>
560
561
562 <p>Note that these rulesets can be used in a <code>.htaccess</code>
563 file, as well as in a &lt;Directory&gt; block.</p>
564
565 </dd>
566
567 </dl>
568
569 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
570 <div class="section">
571 <h2><a name="rewrite-query" id="rewrite-query">Rewrite query string</a></h2>
572
573
574 <dl>
575 <dt>Description:</dt>
576 <dd>You want to capture a particular value from a query string
577 and either replace it or incorporate it into another component
578 of the URL.</dd>
579
580 <dt>Solutions:</dt>
581 <dd>
582 <p> Many of the solutions in this section will all use the same condition,
583 which leaves the matched value in the %2 backreference.  %1 is the beginining
584 of the query string (up to the key of intererest), and %3 is the remainder. This
585 condition is a bit complex for flexibility and to avoid double '&amp;&amp;' in the 
586 substitutions.</p>
587 <ul>
588   <li>This solution removes the matching key and value:
589
590 <pre class="prettyprint lang-config"># Remove mykey=???
591 RewriteCond %{QUERY_STRING} (.*(?:^|&amp;))mykey=([^&amp;]*)&amp;?(.*)&amp;?$
592 RewriteRule (.*) $1?%1%3</pre>
593
594   </li>
595
596   <li>This solution uses the captured value in the URL subsitution,
597   discarding the rest of the original query by appending a '?':
598
599 <pre class="prettyprint lang-config"># Copy from query string to PATH_INFO
600 RewriteCond %{QUERY_STRING} (.*(?:^|&amp;))mykey=([^&amp;]*)&amp;?(.*)&amp;?$
601 RewriteRule (.*) $1/products/%2/? [PT]</pre>
602
603   </li>
604
605   <li>This solution checks the captured value in a subsequent condition:
606
607 <pre class="prettyprint lang-config"># Capture the value of mykey in the query string
608 RewriteCond %{QUERY_STRING} (.*(?:^|&amp;))mykey=([^&amp;]*)&amp;?(.*)&amp;?$
609 RewriteCond %2 !=not-so-secret-value 
610 RewriteRule (.*) - [F]</pre>
611
612   </li>
613
614   <li>This solution shows the reverse of the previous ones, copying
615       path components (perhaps PATH_INFO) from the URL into the query string.
616 <pre class="prettyprint lang-config"># The desired URL might be /products/kitchen-sink, and the script expects 
617 # /path?products=kitchen-sink.
618 RewriteRule ^/?path/([^/]+)/([^/]+) /path?$1=$2 [PT]</pre>
619
620   </li>
621 </ul>
622
623 </dd>
624
625 </dl>
626 </div></div>
627 <div class="bottomlang">
628 <p><span>Available Languages: </span><a href="../en/rewrite/remapping.html" title="English">&nbsp;en&nbsp;</a></p>
629 </div><div class="top"><a href="#page-header"><img src="../images/up.gif" alt="top" /></a></div><div class="section"><h2><a id="comments_section" name="comments_section">Comments</a></h2><div class="warning"><strong>Notice:</strong><br />This is not a Q&amp;A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our <a href="http://httpd.apache.org/lists.html">mailing lists</a>.</div>
630 <script type="text/javascript"><!--//--><![CDATA[//><!--
631 var comments_shortname = 'httpd';
632 var comments_identifier = 'http://httpd.apache.org/docs/trunk/rewrite/remapping.html';
633 (function(w, d) {
634     if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
635         d.write('<div id="comments_thread"><\/div>');
636         var s = d.createElement('script');
637         s.type = 'text/javascript';
638         s.async = true;
639         s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
640         (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
641     }
642     else {
643         d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
644     }
645 })(window, document);
646 //--><!]]></script></div><div id="footer">
647 <p class="apache">Copyright 2015 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>
648 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!--
649 if (typeof(prettyPrint) !== 'undefined') {
650     prettyPrint();
651 }
652 //--><!]]></script>
653 </body></html>