]> granicus.if.org Git - apache/blob - docs/manual/rewrite/avoid.xml
Rebuild.
[apache] / docs / manual / rewrite / avoid.xml
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
5
6 <!--
7  Licensed to the Apache Software Foundation (ASF) under one or more
8  contributor license agreements.  See the NOTICE file distributed with
9  this work for additional information regarding copyright ownership.
10  The ASF licenses this file to You under the Apache License, Version 2.0
11  (the "License"); you may not use this file except in compliance with
12  the License.  You may obtain a copy of the License at
13
14      http://www.apache.org/licenses/LICENSE-2.0
15
16  Unless required by applicable law or agreed to in writing, software
17  distributed under the License is distributed on an "AS IS" BASIS,
18  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  See the License for the specific language governing permissions and
20  limitations under the License.
21 -->
22
23 <manualpage metafile="avoid.xml.meta">
24   <parentdocument href="./">Rewrite</parentdocument>
25
26 <title>When not to use mod_rewrite</title>
27
28 <summary>
29
30 <p>This document supplements the <module>mod_rewrite</module>
31 <a href="../mod/mod_rewrite.html">reference documentation</a>. It describes
32 perhaps one of the most important concepts about <module>mod_rewrite</module> - namely,
33 when to avoid using it.</p>
34
35 <p><module>mod_rewrite</module> should be considered a last resort, when other
36 alternatives are found wanting. Using it when there are simpler
37 alternatives leads to configurations which are confusing, fragile, and
38 hard to maintain. Understanding what other alternatives are available is
39 a very important step towards <module>mod_rewrite</module> mastery.</p>
40
41 <p>Note that many of these examples won't work unchanged in your
42 particular server configuration, so it's important that you understand
43 them, rather than merely cutting and pasting the examples into your
44 configuration.</p>
45
46 <p>The most common situation in which <module>mod_rewrite</module> is
47 the right tool is when the very best solution requires access to the
48 server configuration files, and you don't have that access. Some
49 configuration directives are only available in the server configuration
50 file. So if you are in a hosting situation where you only have .htaccess
51 files to work with, you may need to resort to
52 <module>mod_rewrite</module>.</p>
53
54 </summary>
55 <seealso><a href="../mod/mod_rewrite.html">Module documentation</a></seealso>
56 <seealso><a href="intro.html">mod_rewrite introduction</a></seealso>
57 <seealso><a href="remapping.html">Redirection and remapping</a></seealso>
58 <seealso><a href="access.html">Controlling access</a></seealso>
59 <seealso><a href="vhosts.html">Virtual hosts</a></seealso>
60 <seealso><a href="proxy.html">Proxying</a></seealso>
61 <seealso><a href="rewritemap.html">Using RewriteMap</a></seealso>
62 <seealso><a href="advanced.html">Advanced techniques</a></seealso>
63 <!--<seealso><a href="avoid.html">When not to use mod_rewrite</a></seealso>-->
64
65 <section id="redirect">
66 <title>Simple Redirection</title>
67
68 <p><module>mod_alias</module> provides the <directive
69 module="mod_alias">Redirect</directive> and <directive
70 module="mod_alias">RedirectMatch</directive> directives, which provide a
71 means to redirect one URL to another. This kind of simple redirection of
72 one URL, or a class of URLs, to somewhere else, should be accomplished
73 using these directives rather than <directive
74 module="mod_rewrite">RewriteRule</directive>. <code>RedirectMatch</code>
75 allows you to include a regular expression in your redirection criteria,
76 providing many of the benefits of using <code>RewriteRule</code>.</p>
77
78 <p>A common use for <code>RewriteRule</code> is to redirect an entire
79 class of URLs. For example, all URLs in the <code>/one</code> directory
80 must be redirected to <code>http://one.example.com/</code>, or perhaps
81 all <code>http</code> requests must be redirected to
82 <code>https</code>.</p>
83
84 <p>These situations are better handled by the <code>Redirect</code>
85 directive. Remember that <code>Redirect</code> preserves path
86 information. That is to say, a redirect for a URL <code>/one</code> will
87 also redirect all URLs under that, such as <code>/one/two.html</code>
88 and <code>/one/three/four.html</code>.</p>
89
90 <p>To redirect URLs under <code>/one</code> to
91 <code>http://one.example.com</code>, do the following:</p>
92
93 <highlight language="config">
94 Redirect "/one/" "http://one.example.com/"
95 </highlight>
96
97 <p>To redirect one hostname to another, for example
98 <code>example.com</code> to <code>www.example.com</code>, see the
99 <a href="remapping.html#canonicalhost">Canonical Hostnames</a>
100 recipe.</p>
101
102 <p>To redirect <code>http</code> URLs to <code>https</code>, do the
103 following:</p>
104
105 <highlight language="config">
106 &lt;VirtualHost *:80&gt;
107     ServerName www.example.com
108     Redirect "/" "https://www.example.com/"
109 &lt;/VirtualHost&gt;
110
111 &lt;VirtualHost *:443&gt;
112     ServerName www.example.com
113     # ... SSL configuration goes here
114 &lt;/VirtualHost&gt;
115 </highlight>
116
117 <p>The use of <code>RewriteRule</code> to perform this task may be
118 appropriate if there are other <code>RewriteRule</code> directives in
119 the same scope. This is because, when there are <code>Redirect</code>
120 and <code>RewriteRule</code> directives in the same scope, the
121 <code>RewriteRule</code> directives will run first, regardless of the
122 order of appearance in the configuration file.</p>
123
124 <p>In the case of the <em>http-to-https</em> redirection, the use of
125 <code>RewriteRule</code> would be appropriate if you don't have access
126 to the main server configuration file, and are obliged to perform this
127 task in a <code>.htaccess</code> file instead.</p>
128
129 </section>
130
131 <section id="alias"><title>URL Aliasing</title>
132 <p>The <directive module="mod_alias">Alias</directive> directive
133 provides mapping from a URI to a directory - usually a directory outside
134 of your <directive module="core">DocumentRoot</directive>. Although it
135 is possible to perform this mapping with <module>mod_rewrite</module>,
136 <directive module="mod_alias">Alias</directive> is the preferred method, for
137 reasons of simplicity and performance.</p>
138
139 <example><title>Using Alias</title>
140 <highlight language="config">
141 Alias "/cats" "/var/www/virtualhosts/felines/htdocs"
142 </highlight>
143 </example>
144
145 <p>
146 The use of <module>mod_rewrite</module> to perform this mapping may be
147 appropriate when you do not have access to the server configuration
148 files. Alias may only be used in server or virtualhost context, and not
149 in a <code>.htaccess</code> file.
150 </p>
151
152 <p>Symbolic links would be another way to accomplish the same thing, if
153 you have <code>Options FollowSymLinks</code> enabled on your
154 server.</p>
155 </section>
156
157 <section id="vhosts"><title>Virtual Hosting</title>
158 <p>Although it is possible to handle <a href="vhosts.html">virtual hosts
159 with mod_rewrite</a>, it is seldom the right way. Creating individual
160 <directive module="core" type="section">VirtualHost</directive> blocks is
161 almost always the right way to go. In the
162 event that you have an enormous number of virtual hosts, consider using
163 <module>mod_vhost_alias</module> to create these hosts automatically.</p>
164
165 <p>Modules such as <module>mod_macro</module> are
166 also useful for creating a large number of virtual hosts dynamically.</p>
167
168 <p>Using <module>mod_rewrite</module> for vitualhost creation may be
169 appropriate if you are using a hosting service that does not provide
170 you access to the server configuration files, and you are therefore
171 restricted to configuration using <code>.htaccess</code> files.</p>
172
173 <p>See the <a href="vhosts.html">virtual hosts with mod_rewrite</a>
174 document for more details on how you might accomplish this if it still
175 seems like the right approach.</p>
176
177 </section>
178
179 <section id="proxy"><title>Simple Proxying</title>
180
181 <p><directive module="mod_rewrite">RewriteRule</directive> provides the <a
182 href="flags.html#flag_p">[P]</a> flag to pass rewritten URIs through
183 <module>mod_proxy</module>.</p>
184
185 <highlight language="config">
186 RewriteRule "^/?images(.*)" "http://imageserver.local/images$1" [P]
187 </highlight>
188
189 <p>However, in many cases, when there is no actual pattern matching
190 needed, as in the example shown above, the <directive
191 module="mod_proxy">ProxyPass</directive> directive is a better choice.
192 The example here could be rendered as:</p>
193
194 <highlight language="config">
195 ProxyPass "/images/" "http://imageserver.local/images/"
196 </highlight>
197
198 <p>Note that whether you use <directive
199 module="mod_rewrite">RewriteRule</directive> or <directive
200 module="mod_proxy">ProxyPass</directive>, you'll still need to use the
201 <directive module="mod_proxy">ProxyPassReverse</directive> directive to
202 catch redirects issued from the back-end server:</p>
203
204 <highlight language="config">
205 ProxyPassReverse "/images/" "http://imageserver.local/images/"
206 </highlight>
207
208 <p>You may need to use <code>RewriteRule</code> instead when there are
209 other <code>RewriteRule</code>s in effect in the same scope, as a
210 <code>RewriteRule</code> will usually take effect before a
211 <code>ProxyPass</code>, and so may preempt what you're trying to
212 accomplish.</p>
213
214 </section>
215
216 <section id="setenv"><title>Environment Variable Testing</title>
217
218 <p><module>mod_rewrite</module> is frequently used to take a particular
219 action based on the presence or absence of a particular environment
220 variable or request header. This can be done more efficiently using the
221 <directive module="core" type="section">If</directive>.</p>
222
223 <p>Consider, for example, the common scenario where
224 <directive>RewriteRule</directive> is used to enforce a canonical
225 hostname, such as <code>www.example.com</code> instead of
226 <code>example.com</code>. This can be done using the <directive
227 module="core" type="section">If</directive> directive, as shown here:</p>
228
229 <highlight language="config">
230 &lt;If "req('Host') != 'www.example.com'"&gt;
231     Redirect "/" "http://www.example.com/"
232 &lt;/If&gt;
233 </highlight>
234
235 <p>This technique can be used to take actions based on any request
236 header, response header, or environment variable, replacing
237 <module>mod_rewrite</module> in many common scenarios.</p>
238
239 <p>See especially the <a href="../expr.html">expression evaluation
240 documentation</a> for a overview of what types of expressions you can
241 use in <directive module="core" type="section">If</directive> sections,
242 and in certain other directives.</p>
243
244 </section>
245
246 </manualpage>
247