]> granicus.if.org Git - apache/blob - docs/manual/rewrite/vhosts.xml
Updates one of the "advanced" recipes and rewrites description.
[apache] / docs / manual / rewrite / vhosts.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="vhosts.xml.meta">
24   <parentdocument href="./">Rewrite</parentdocument>
25
26 <title>Dynamic mass virtual hosts with 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 how you can use <module>mod_rewrite</module> to create dynamically 
33 configured virtual hosts.</p>
34
35 <note type="warning">mod_rewrite is not the best way to configure
36 virtual hosts. You should first consider the <a
37 href="../vhosts/mass.html">alternatives</a> before resorting to
38 mod_rewrite. See also the "<a href="avoid.html#vhosts">how to avoid
39 mod_rewrite</a> document.</note>
40
41 </summary>
42 <seealso><a href="../mod/mod_rewrite.html">Module documentation</a></seealso>
43 <seealso><a href="intro.html">mod_rewrite introduction</a></seealso>
44 <seealso><a href="remapping.html">Redirection and remapping</a></seealso>
45 <seealso><a href="access.html">Controlling access</a></seealso>
46 <!--<seealso><a href="vhosts.html">Virtual hosts</a></seealso>-->
47 <seealso><a href="proxy.html">Proxying</a></seealso>
48 <seealso><a href="rewritemap.html">RewriteMap</a></seealso>
49 <seealso><a href="advanced.html">Advanced techniques</a></seealso>
50 <seealso><a href="avoid.html">When not to use mod_rewrite</a></seealso>
51
52 <section id="per-hostname">
53
54   <title>Virtual Hosts For Arbitrary Hostnames</title>
55
56   <dl>
57     <dt>Description:</dt>
58
59     <dd>
60     <p>We want to automatically create a virtual host for every hostname
61     which resolves in our domain, without having to create
62     new VirtualHost sections.</p>
63
64     <p>In this recipe, we assume that we'll be using the hostname
65     <code>www.<strong>SITE</strong>.example.com</code> for each
66     user, and serve their content out of
67     <code>/home/<strong>SITE</strong>/www</code>.</p>
68     </dd>
69
70     <dt>Solution:</dt>
71
72     <dd>
73
74 <example>
75 RewriteEngine on<br />
76 <br />
77 RewriteMap    lowercase int:tolower<br />
78 <br />
79 RewriteCond   %{lowercase:%{<strong>HTTP_HOST</strong>}}   ^www\.<strong>([^.]+)</strong>\.example\.com$<br />
80 RewriteRule   ^(.*) /home/<strong>%1</strong>/www$1
81 </example></dd>
82
83 <dt>Discussion</dt>
84     <dd>
85
86     <note type="warning">You will need to take care of the DNS 
87     resolution - Apache does
88     not handle name resolution. You'll need either to create CNAME 
89     records for each hostname, or a DNS wildcard record. Creating DNS
90     records is beyond the scope of this document.</note>
91
92 <p>The internal <code>tolower</code> RewriteMap directive is used to
93 ensure that the hostnames being used are all lowercase, so that there is
94 no ambiguity in the directory structure which must be created.</p>
95
96 <p>Parentheses used in a <directive
97 module="mod_rewrite">RewriteCond</directive> are captured into the
98 backreferences <code>%1</code>, <code>%2</code>, etc, while parentheses
99 used in <directive module="mod_rewrite">RewriteRule</directive> are
100 captured into the backreferences <code>$1</code>, <code>$2</code>,
101 etc.</p>
102
103 <p>
104 As with many techniques discussed in this document, mod_rewrite really
105 isn't the best way to accomplish this task. You should, instead,
106 consider using <module>mod_vhost_alias</module> instead, as it will much
107 more gracefully handle anything beyond serving static files, such as any
108 dynamic content, and Alias resolution. 
109 </p>
110     </dd>
111   </dl>
112
113 </section>
114
115 <section id="simple.rewrite"><title>Dynamic
116     Virtual Hosts Using <module>mod_rewrite</module></title>
117
118     <p>This extract from <code>httpd.conf</code> does the same
119     thing as <a href="#simple">the first example</a>. The first
120     half is very similar to the corresponding part above, except for
121     some changes, required for backward compatibility and to make the
122     <code>mod_rewrite</code> part work properly; the second half
123     configures <code>mod_rewrite</code> to do the actual work.</p>
124
125     <p>Because <code>mod_rewrite</code> runs before other URI translation
126     modules (e.g., <code>mod_alias</code>), <code>mod_rewrite</code> must
127     be told to explicitly ignore any URLs that would have been handled
128     by those modules. And, because these rules would otherwise bypass
129     any <code>ScriptAlias</code> directives, we must have
130     <code>mod_rewrite</code> explicitly enact those mappings.</p>
131
132 <example>
133 # get the server name from the Host: header<br />
134 UseCanonicalName Off<br />
135 <br />
136 # splittable logs<br />
137 LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon<br />
138 CustomLog logs/access_log vcommon<br />
139 <br />
140 &lt;Directory /www/hosts&gt;<br />
141 <indent>
142     # ExecCGI is needed here because we can't force<br />
143     # CGI execution in the way that ScriptAlias does<br />
144     Options FollowSymLinks ExecCGI<br />
145 </indent>
146 &lt;/Directory&gt;<br />
147 <br />
148 RewriteEngine On<br />
149 <br />
150 # a ServerName derived from a Host: header may be any case at all<br />
151 RewriteMap  lowercase  int:tolower<br />
152 <br />
153 ## deal with normal documents first:<br />
154 # allow Alias /icons/ to work - repeat for other aliases<br />
155 RewriteCond  %{REQUEST_URI}  !^/icons/<br />
156 # allow CGIs to work<br />
157 RewriteCond  %{REQUEST_URI}  !^/cgi-bin/<br />
158 # do the magic<br />
159 RewriteRule  ^/(.*)$  /www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1<br />
160 <br />
161 ## and now deal with CGIs - we have to force a handler<br />
162 RewriteCond  %{REQUEST_URI}  ^/cgi-bin/<br />
163 RewriteRule  ^/(.*)$  /www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1  [H=cgi-script]<br />
164 </example>
165
166 </section>
167
168 <section id="xtra-conf"><title>Using a Separate Virtual Host Configuration File</title>
169
170     <p>This arrangement uses more advanced <module>mod_rewrite</module>
171     features to work out the translation from virtual host to document
172     root, from a separate configuration file. This provides more
173     flexibility, but requires more complicated configuration.</p>
174
175     <p>The <code>vhost.map</code> file should look something like
176     this:</p>
177
178 <example>
179 customer-1.example.com  /www/customers/1<br />
180 customer-2.example.com  /www/customers/2<br />
181 # ...<br />
182 customer-N.example.com  /www/customers/N<br />
183 </example>
184
185     <p>The <code>httpd.conf</code> should contain the following:</p>
186
187 <example>
188 RewriteEngine on<br />
189 <br />
190 RewriteMap   lowercase  int:tolower<br />
191 <br />
192 # define the map file<br />
193 RewriteMap   vhost      txt:/www/conf/vhost.map<br />
194 <br />
195 # deal with aliases as above<br />
196 RewriteCond  %{REQUEST_URI}               !^/icons/<br />
197 RewriteCond  %{REQUEST_URI}               !^/cgi-bin/<br />
198 RewriteCond  ${lowercase:%{SERVER_NAME}}  ^(.+)$<br />
199 # this does the file-based remap<br />
200 RewriteCond  ${vhost:%1}                  ^(/.*)$<br />
201 RewriteRule  ^/(.*)$                      %1/docs/$1<br />
202 <br />
203 RewriteCond  %{REQUEST_URI}               ^/cgi-bin/<br />
204 RewriteCond  ${lowercase:%{SERVER_NAME}}  ^(.+)$<br />
205 RewriteCond  ${vhost:%1}                  ^(/.*)$<br />
206 RewriteRule  ^/(.*)$                      %1/cgi-bin/$1 [H=cgi-script]
207 </example>
208
209 </section>
210
211 </manualpage>