]> granicus.if.org Git - apache/blob - docs/manual/rewrite/vhosts.xml
Enclose parameters in quotation marks for <{Files,Directory,Location}{,Match}>
[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 <highlight language="config">
75 RewriteEngine on
76
77 RewriteMap    lowercase int:tolower
78
79 RewriteCond   ${lowercase:%{<strong>HTTP_HOST</strong>}}   ^www\.<strong>([^.]+)</strong>\.example\.com$
80 RewriteRule   ^(.*) /home/<strong>%1</strong>/www$1
81 </highlight></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="#per-hostname">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 <highlight language="config">
133 # get the server name from the Host: header
134 UseCanonicalName Off
135
136 # splittable logs
137 LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
138 CustomLog logs/access_log vcommon
139
140 &lt;Directory "/www/hosts"&gt;
141     # ExecCGI is needed here because we can't force
142     # CGI execution in the way that ScriptAlias does
143     Options FollowSymLinks ExecCGI
144 &lt;/Directory&gt;
145
146 RewriteEngine On
147
148 # a ServerName derived from a Host: header may be any case at all
149 RewriteMap  lowercase  int:tolower
150
151 ## deal with normal documents first:
152 # allow Alias /icons/ to work - repeat for other aliases
153 RewriteCond  %{REQUEST_URI}  !^/icons/
154 # allow CGIs to work
155 RewriteCond  %{REQUEST_URI}  !^/cgi-bin/
156 # do the magic
157 RewriteRule  ^/(.*)$  /www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1
158
159 ## and now deal with CGIs - we have to force a handler
160 RewriteCond  %{REQUEST_URI}  ^/cgi-bin/
161 RewriteRule  ^/(.*)$  /www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1  [H=cgi-script]
162 </highlight>
163
164 </section>
165
166 <section id="xtra-conf"><title>Using a Separate Virtual Host Configuration File</title>
167
168     <p>This arrangement uses more advanced <module>mod_rewrite</module>
169     features to work out the translation from virtual host to document
170     root, from a separate configuration file. This provides more
171     flexibility, but requires more complicated configuration.</p>
172
173     <p>The <code>vhost.map</code> file should look something like
174     this:</p>
175
176 <example>
177 customer-1.example.com  /www/customers/1<br />
178 customer-2.example.com  /www/customers/2<br />
179 # ...<br />
180 customer-N.example.com  /www/customers/N<br />
181 </example>
182
183     <p>The <code>httpd.conf</code> should contain the following:</p>
184
185 <highlight language="config">
186 RewriteEngine on
187
188 RewriteMap   lowercase  int:tolower
189
190 # define the map file
191 RewriteMap   vhost      txt:/www/conf/vhost.map
192
193 # deal with aliases as above
194 RewriteCond  %{REQUEST_URI}               !^/icons/
195 RewriteCond  %{REQUEST_URI}               !^/cgi-bin/
196 RewriteCond  ${lowercase:%{SERVER_NAME}}  ^(.+)$
197 # this does the file-based remap
198 RewriteCond  ${vhost:%1}                  ^(/.*)$
199 RewriteRule  ^/(.*)$                      %1/docs/$1
200
201 RewriteCond  %{REQUEST_URI}               ^/cgi-bin/
202 RewriteCond  ${lowercase:%{SERVER_NAME}}  ^(.+)$
203 RewriteCond  ${vhost:%1}                  ^(/.*)$
204 RewriteRule  ^/(.*)$                      %1/cgi-bin/$1 [H=cgi-script]
205 </highlight>
206
207 </section>
208
209 </manualpage>