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