]> granicus.if.org Git - apache/blob - docs/manual/rewrite/avoid.xml
Re-order listing of pages more logically.
[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 mod_rewrite - namely,
33 when to avoid using it.</p>
34
35 <p>mod_rewrite 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 mod_rewrite 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 <code>mod_rewrite</code>,
136 <code>Alias</code> is the preferred method, for reasons of simplicity
137 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 <code>mod_rewrite</code> 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 &lt;VirtualHost&gt; blocks is almost always the right way to go. In the
161 event that you have an enormous number of virtual hosts, consider using
162 <module>mod_vhost_alias</module> to create these hosts automatically.</p>
163
164 <p>Modules such as <module>mod_macro</module> are
165 also useful for creating a large number of virtual hosts dynamically.</p>
166
167 <p>Using <module>mod_rewrite</module> for vitualhost creation may be
168 appropriate if you are using a hosting service that does not provide
169 you access to the server configuration files, and you are therefore
170 restricted to configuration using <code>.htaccess</code> files.</p>
171
172 <p>See the <a href="vhosts.html">virtual hosts with mod_rewrite</a>
173 document for more details on how you might accomplish this if it still
174 seems like the right approach.</p>
175
176 </section>
177
178 <section id="proxy"><title>Simple Proxying</title>
179
180 <p><code>RewriteRule</code> provides the <a
181 href="flags.html#flag_p">[P]</a> flag to pass rewritten URIs through
182 <module>mod_proxy</module>.</p>
183
184 <highlight language="config">
185 RewriteRule "^/?images(.*)" "http://imageserver.local/images$1" [P]
186 </highlight>
187
188 <p>However, in many cases, when there is no actual pattern matching
189 needed, as in the example shown above, the <directive
190 module="mod_proxy">ProxyPass</directive> directive is a better choice.
191 The example here could be rendered as:</p>
192
193 <highlight language="config">
194 ProxyPass "/images/" "http://imageserver.local/images/"
195 </highlight>
196
197 <p>Note that whether you use <directive
198 module="mod_rewrite">RewriteRule</directive> or <directive
199 module="mod_proxy">ProxyPass</directive>, you'll still need to use the
200 <directive module="mod_proxy">ProxyPassReverse</directive> directive to
201 catch redirects issued from the back-end server:</p>
202
203 <highlight language="config">
204 ProxyPassReverse "/images/" "http://imageserver.local/images/"
205 </highlight>
206
207 <p>You may need to use <code>RewriteRule</code> instead when there are
208 other <code>RewriteRule</code>s in effect in the same scope, as a
209 <code>RewriteRule</code> will usually take effect before a
210 <code>ProxyPass</code>, and so may preempt what you're trying to
211 accomplish.</p>
212
213 </section>
214
215 <section id="setenv"><title>Environment Variable Testing</title>
216
217 <p><module>mod_rewrite</module> is frequently used to take a particular
218 action based on the presence or absence of a particular environment
219 variable or request header. This can be done more efficiently using the
220 <directive module="core" type="section">If</directive>.</p>
221
222 <p>Consider, for example, the common scenario where
223 <directive>RewriteRule</directive> is used to enforce a canonical
224 hostname, such as <code>www.example.com</code> instead of
225 <code>example.com</code>. This can be done using the <directive
226 module="core" type="section">If</directive> directive, as shown here:</p>
227
228 <highlight language="config">
229 &lt;If "req('Host') != 'www.example.com'"&gt;
230     Redirect "/" "http://www.example.com/"
231 &lt;/If&gt;
232 </highlight>
233
234 <p>This technique can be used to take actions based on any request
235 header, response header, or environment variable, replacing
236 <module>mod_rewrite</module> in many common scenarios.</p>
237
238 <p>See especially the <a href="../expr.html">expression evaluation
239 documentation</a> for a overview of what types of expressions you can
240 use in &lt;If&gt; sections, and in certain other directives.</p>
241
242 </section>
243
244 </manualpage>
245