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$ -->
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
14 http://www.apache.org/licenses/LICENSE-2.0
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.
23 <manualpage metafile="vhosts.xml.meta">
24 <parentdocument href="./">Rewrite</parentdocument>
26 <title>Dynamic mass virtual hosts with mod_rewrite</title>
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>
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>
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 and tricks</a></seealso>
50 <seealso><a href="avoid.html">When not to use mod_rewrite</a></seealso>
52 <section id="per-hostname">
54 <title>Virtual Hosts For Arbitrary Hostnames</title>
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>
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>
77 RewriteMap lowercase int:tolower
79 RewriteCond %{lowercase:%{<strong>HTTP_HOST</strong>}} ^www\.<strong>([^.]+)</strong>\.example\.com$
80 RewriteRule ^(.*) /home/<strong>%1</strong>/www$1
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>
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>
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>,
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.
115 <section id="simple.rewrite"><title>Dynamic
116 Virtual Hosts Using <module>mod_rewrite</module></title>
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>
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>
133 # get the server name from the Host: header<br />
134 UseCanonicalName Off<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 />
140 <Directory /www/hosts><br />
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 />
146 </Directory><br />
148 RewriteEngine On<br />
150 # a ServerName derived from a Host: header may be any case at all<br />
151 RewriteMap lowercase int:tolower<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 />
159 RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1<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 />
168 <section id="xtra-conf"><title>Using a Separate Virtual Host Configuration File</title>
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>
175 <p>The <code>vhost.map</code> file should look something like
179 www.customer-1.com /www/customers/1<br />
180 www.customer-2.com /www/customers/2<br />
182 www.customer-N.com /www/customers/N<br />
185 <p>The <code>httpd.conf</code> should contain the following:</p>
188 RewriteEngine on<br />
190 RewriteMap lowercase int:tolower<br />
192 # define the map file<br />
193 RewriteMap vhost txt:/www/conf/vhost.map<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 />
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]