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