avoid.html.en revision 181523b7d40e95e62d1b041846436ce1e7348e88
97a9a944b5887e91042b019776c41d5dd74557aferikabele<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
97a9a944b5887e91042b019776c41d5dd74557aferikabele<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
8c81ebbdeed67059e7a1106d7c617543e901c3abyoshiki XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
8c81ebbdeed67059e7a1106d7c617543e901c3abyoshiki This file is generated from xml source: DO NOT EDIT
8c81ebbdeed67059e7a1106d7c617543e901c3abyoshiki XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
96ad5d81ee4a2cc66a4ae19893efc8aa6d06fae7jailletc<title>When not to use mod_rewrite - Apache HTTP Server</title>
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nd<link href="/style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nd<link href="/style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen<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" />
2e545ce2450a9953665f701bb05350f0d3f26275nd<script src="/style/scripts/prettify.js" type="text/javascript">
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nd<link href="/images/favicon.ico" rel="shortcut icon" /></head>
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nd<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>
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nd<div class="up"><a href="./"><img title="<-" alt="<-" src="/images/left.gif" /></a></div>
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nd<a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.5</a> > <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>When not to use mod_rewrite</h1>
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nd<p><span>Available Languages: </span><a href="/en/rewrite/avoid.html" title="English"> en </a></p>
7f5b59ccc63c0c0e3e678a168f09ee6a2f51f9d0nd<p>This document supplements the <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>
e1e8390280254f7f0580d701e583f670643d4f3fnilgun<a href="/mod/mod_rewrite.html">reference documentation</a>. It describes
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjungperhaps one of the most important concepts about mod_rewrite - namely,
3b3b7fc78d1f5bfc2769903375050048ff41ff26ndwhen to avoid using it.</p>
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nd<p>mod_rewrite should be considered a last resort, when other
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4ndalternatives are found wanting. Using it when there are simpler
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4ndalternatives leads to configurations which are confusing, fragile, and
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4ndhard to maintain. Understanding what other alternatives are available is
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nda very important step towards mod_rewrite mastery.</p>
8c81ebbdeed67059e7a1106d7c617543e901c3abyoshiki<p>Note that many of these examples won't work unchanged in your
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4ndparticular server configuration, so it's important that you understand
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4ndthem, rather than merely cutting and pasting the examples into your
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4ndconfiguration.</p>
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nd<p>The most common situation in which <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code> is
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4ndthe right tool is when the very best solution requires access to the
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4ndserver configuration files, and you don't have that access. Some
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4ndconfiguration directives are only available in the server configuration
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4ndfile. So if you are in a hosting situation where you only have .htaccess
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4ndfiles to work with, you may need to resort to
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nd<code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>.</p>
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nd<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#redirect">Simple Redirection</a></li>
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nd<li><img alt="" src="/images/down.gif" /> <a href="#alias">URL Aliasing</a></li>
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nd<li><img alt="" src="/images/down.gif" /> <a href="#vhosts">Virtual Hosting</a></li>
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nd<li><img alt="" src="/images/down.gif" /> <a href="#proxy">Simple Proxying</a></li>
f61ec47ec494705bd2d02b5b6fe021e5c193f2c8noodl<li><img alt="" src="/images/down.gif" /> <a href="#setenv">Environment Variable Testing</a></li>
f61ec47ec494705bd2d02b5b6fe021e5c193f2c8noodl</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="remapping.html">Redirection and remapping</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></ul></div>
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nd<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nd<h2><a name="redirect" id="redirect">Simple Redirection</a></h2>
06ba4a61654b3763ad65f52283832ebf058fdf1cslive<p><code class="module"><a href="/mod/mod_alias.html">mod_alias</a></code> provides the <code class="directive"><a href="/mod/mod_alias.html#redirect">Redirect</a></code> and <code class="directive"><a href="/mod/mod_alias.html#redirectmatch">RedirectMatch</a></code> directives, which provide a
06ba4a61654b3763ad65f52283832ebf058fdf1cslivemeans to redirect one URL to another. This kind of simple redirection of
06ba4a61654b3763ad65f52283832ebf058fdf1csliveone URL, or a class of URLs, to somewhere else, should be accomplished
06ba4a61654b3763ad65f52283832ebf058fdf1csliveusing these directives rather than <code class="directive"><a href="/mod/mod_rewrite.html#rewriterule">RewriteRule</a></code>. <code>RedirectMatch</code>
06ba4a61654b3763ad65f52283832ebf058fdf1csliveallows you to include a regular expression in your redirection criteria,
06ba4a61654b3763ad65f52283832ebf058fdf1csliveproviding many of the benefits of using <code>RewriteRule</code>.</p>
06ba4a61654b3763ad65f52283832ebf058fdf1cslive<p>A common use for <code>RewriteRule</code> is to redirect an entire
06ba4a61654b3763ad65f52283832ebf058fdf1csliveclass of URLs. For example, all URLs in the <code>/one</code> directory
06ba4a61654b3763ad65f52283832ebf058fdf1cslivemust be redirected to <code>http://one.example.com/</code>, or perhaps
06ba4a61654b3763ad65f52283832ebf058fdf1cslive<p>These situations are better handled by the <code>Redirect</code>
06ba4a61654b3763ad65f52283832ebf058fdf1cslivedirective. Remember that <code>Redirect</code> preserves path
06ba4a61654b3763ad65f52283832ebf058fdf1csliveinformation. That is to say, a redirect for a URL <code>/one</code> will
06ba4a61654b3763ad65f52283832ebf058fdf1cslivealso redirect all URLs under that, such as <code>/one/two.html</code>
06ba4a61654b3763ad65f52283832ebf058fdf1cslive<code>http://one.example.com</code>, do the following:</p>
06ba4a61654b3763ad65f52283832ebf058fdf1cslive<pre class="prettyprint lang-config">Redirect /one/ http://one.example.com/</pre>
06ba4a61654b3763ad65f52283832ebf058fdf1cslive<p>To redirect <code>http</code> URLs to <code>https</code>, do the
06ba4a61654b3763ad65f52283832ebf058fdf1cslivefollowing:</p>
06ba4a61654b3763ad65f52283832ebf058fdf1cslive<VirtualHost *:80>
82178a3043043b8813c0d7288a06ca1b7d110d4atakashi</VirtualHost >
06ba4a61654b3763ad65f52283832ebf058fdf1cslive<VirtualHost *:443>
e1e8390280254f7f0580d701e583f670643d4f3fnilgun # ... SSL configuration goes here
e1e8390280254f7f0580d701e583f670643d4f3fnilgun</VirtualHost >
e1e8390280254f7f0580d701e583f670643d4f3fnilgun<p>The use of <code>RewriteRule</code> to perform this task may be
e1e8390280254f7f0580d701e583f670643d4f3fnilgunappropriate if there are other <code>RewriteRule</code> directives in
e1e8390280254f7f0580d701e583f670643d4f3fnilgunthe same scope. This is because, when there are <code>Redirect</code>
e1e8390280254f7f0580d701e583f670643d4f3fnilgunand <code>RewriteRule</code> directives in the same scope, the
e1e8390280254f7f0580d701e583f670643d4f3fnilgun<code>RewriteRule</code> directives will run first, regardless of the
e1e8390280254f7f0580d701e583f670643d4f3fnilgunorder of appearance in the configuration file.</p>
e1e8390280254f7f0580d701e583f670643d4f3fnilgun<p>In the case of the <em>http-to-https</em> redirection, the use of
e1e8390280254f7f0580d701e583f670643d4f3fnilgun<code>RewriteRule</code> would be appropriate if you don't have access
e1e8390280254f7f0580d701e583f670643d4f3fnilgunto the main server configuration file, and are obliged to perform this
e1e8390280254f7f0580d701e583f670643d4f3fnilgun</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
e1e8390280254f7f0580d701e583f670643d4f3fnilgun<p>The <code class="directive"><a href="/mod/mod_alias.html#alias">Alias</a></code> directive
e1e8390280254f7f0580d701e583f670643d4f3fnilgunprovides mapping from a URI to a directory - usually a directory outside
e1e8390280254f7f0580d701e583f670643d4f3fnilgunof your <code class="directive"><a href="/mod/core.html#documentroot">DocumentRoot</a></code>. Although it
e1e8390280254f7f0580d701e583f670643d4f3fnilgunis possible to perform this mapping with <code>mod_rewrite</code>,
06ba4a61654b3763ad65f52283832ebf058fdf1cslive<code>Alias</code> is the preferred method, for reasons of simplicity
3a6531f1fd50efd78381e5800802a1449096781esliveand performance.</p>
97a9a944b5887e91042b019776c41d5dd74557aferikabele<pre class="prettyprint lang-config">Alias /cats /var/www/virtualhosts/felines/htdocs</pre>
06ba4a61654b3763ad65f52283832ebf058fdf1csliveThe use of <code>mod_rewrite</code> to perform this mapping may be
e1e8390280254f7f0580d701e583f670643d4f3fnilgunappropriate when you do not have access to the server configuration
e1e8390280254f7f0580d701e583f670643d4f3fnilgunfiles. Alias may only be used in server or virtualhost context, and not
17ade6df5ec233536985eb1c130a906c725dd614humbedooh<p>Symbolic links would be another way to accomplish the same thing, if
06ba4a61654b3763ad65f52283832ebf058fdf1csliveyou have <code>Options FollowSymLinks</code> enabled on your
e1e8390280254f7f0580d701e583f670643d4f3fnilgun</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
17ade6df5ec233536985eb1c130a906c725dd614humbedooh<p>Although it is possible to handle <a href="vhosts.html">virtual hosts
06ba4a61654b3763ad65f52283832ebf058fdf1cslivewith mod_rewrite</a>, it is seldom the right way. Creating individual
e1e8390280254f7f0580d701e583f670643d4f3fnilgun<VirtualHost> blocks is almost always the right way to go. In the
e1e8390280254f7f0580d701e583f670643d4f3fnilgunevent that you have an enormous number of virtual hosts, consider using
e06675c51d084791089d79c3ac18aeae8dd465fcrbb<code class="module"><a href="/mod/mod_vhost_alias.html">mod_vhost_alias</a></code> to create these hosts automatically.</p>
17ade6df5ec233536985eb1c130a906c725dd614humbedooh<p>Third-party modules such as <a href="http://www.cri.ensmp.fr/~coelho/mod_macro/">mod_macro</a> are
e1e8390280254f7f0580d701e583f670643d4f3fnilgunalso useful for creating a large number of virtual hosts dynamically.</p>
e1e8390280254f7f0580d701e583f670643d4f3fnilgun<p>Using <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code> for vitualhost creation may be
e1e8390280254f7f0580d701e583f670643d4f3fnilgunappropriate if you are using a hosting service that does not provide
e1e8390280254f7f0580d701e583f670643d4f3fnilgunyou access to the server configuration files, and you are therefore
e1e8390280254f7f0580d701e583f670643d4f3fnilgunrestricted to configuration using <code>.htaccess</code> files.</p>
e1e8390280254f7f0580d701e583f670643d4f3fnilgun<p>See the <a href="vhosts.html">virtual hosts with mod_rewrite</a>
2f46ce2a814d7f2b126dfb9f1b25fd64e2fbdc11rbowendocument for more details on how you might accomplish this if it still
e1e8390280254f7f0580d701e583f670643d4f3fnilgunseems like the right approach.</p>
e1e8390280254f7f0580d701e583f670643d4f3fnilgun</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
e1e8390280254f7f0580d701e583f670643d4f3fnilgun<h2><a name="proxy" id="proxy">Simple Proxying</a></h2>
e1e8390280254f7f0580d701e583f670643d4f3fnilgun<p><code>RewriteRule</code> provides the <a href="flags.html#flag_p">[P]</a> flag to pass rewritten URIs through
25d005c58102474e06fb67d748273b161d7198d0pquerna<code class="module"><a href="/mod/mod_proxy.html">mod_proxy</a></code>.</p>
a5a2e98043365d3f5a8bb3c3cdfc252a4f11bf02ndRewriteRule ^/?images(.*) http://imageserver.local/images$1 [P]
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nd<p>However, in many cases, when there is no actual pattern matching
e1e8390280254f7f0580d701e583f670643d4f3fnilgunneeded, as in the example shown above, the <code class="directive"><a href="/mod/mod_proxy.html#proxypass">ProxyPass</a></code> directive is a better choice.
2f46ce2a814d7f2b126dfb9f1b25fd64e2fbdc11rbowenThe example here could be rendered as:</p>
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4ndProxyPass /images/ http://imageserver.local/images/
7f5b59ccc63c0c0e3e678a168f09ee6a2f51f9d0nd<p>Note that whether you use <code class="directive"><a href="/mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> or <code class="directive"><a href="/mod/mod_proxy.html#proxypass">ProxyPass</a></code>, you'll still need to use the
e1e8390280254f7f0580d701e583f670643d4f3fnilgun<code class="directive"><a href="/mod/mod_proxy.html#proxypassreverse">ProxyPassReverse</a></code> directive to
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjungcatch redirects issued from the back-end server:</p>
727872d18412fc021f03969b8641810d8896820bhumbedoohProxyPassReverse /images/ http://imageserver.local/images/
727872d18412fc021f03969b8641810d8896820bhumbedooh<p>You may need to use <code>RewriteRule</code> instead when there are
0d0ba3a410038e179b695446bb149cce6264e0abndother <code>RewriteRule</code>s in effect in the same scope, as a
0d0ba3a410038e179b695446bb149cce6264e0abnd<code>RewriteRule</code> will usually take effect before a
0d0ba3a410038e179b695446bb149cce6264e0abnd<code>ProxyPass</code>, and so may preempt what you're trying to
ac082aefa89416cbdc9a1836eaf3bed9698201c8humbedoohaccomplish.</p>
0d0ba3a410038e179b695446bb149cce6264e0abnd</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
727872d18412fc021f03969b8641810d8896820bhumbedooh<h2><a name="setenv" id="setenv">Environment Variable Testing</a></h2>
0d0ba3a410038e179b695446bb149cce6264e0abnd<p><code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code> is frequently used to take a particular
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedoohaction based on the presence or absence of a particular environment
205f749042ed530040a4f0080dbcb47ceae8a374rjungvariable or request header. This can be done more efficiently using the
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen<code class="directive"><a href="/mod/core.html#if"><If></a></code>.</p>
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd<p>Consider, for example, the common scenario where
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd<code class="directive">RewriteRule</code> is used to enforce a canonical
0c4abc32c00611fe1d52c9661f5cc79a3f74c6d4nd<code>example.com</code>. This can be done using the <code class="directive"><a href="/mod/core.html#if"><If></a></code> directive, as shown here:</p>
<If "$req{Host} != 'www.example.com'">
RedirectMatch (.*) http://www.example.com$1
<code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code> in many common scenarios.</p>
<p><span>Available Languages: </span><a href="/en/rewrite/avoid.html" title="English"> en </a></p>
<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>
<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">