github.com/krum110487/go-htaccess@v0.0.0-20240316004156-60641c8e7598/tests/data/apache_2_2_34/manual/rewrite/remapping.html.en (about)

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