]> granicus.if.org Git - apache/blob - docs/manual/rewrite/vhosts.html.en
Rebuild
[apache] / docs / manual / rewrite / vhosts.html.en
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
4         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5               This file is generated from xml source: DO NOT EDIT
6         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7       -->
8 <title>Dynamic mass virtual hosts with mod_rewrite - Apache HTTP Server</title>
9 <link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
10 <link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
11 <link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
12 <link href="../images/favicon.ico" rel="shortcut icon" /></head>
13 <body id="manual-page"><div id="page-header">
14 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
15 <p class="apache">Apache HTTP Server Version 2.5</p>
16 <img alt="" src="../images/feather.gif" /></div>
17 <div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="../images/left.gif" /></a></div>
18 <div id="path">
19 <a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="../">Version 2.5</a> &gt; <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>Dynamic mass virtual hosts with mod_rewrite</h1>
20 <div class="toplang">
21 <p><span>Available Languages: </span><a href="../en/rewrite/vhosts.html" title="English">&nbsp;en&nbsp;</a></p>
22 </div>
23
24
25 <p>This document supplements the <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
26 <a href="../mod/mod_rewrite.html">reference documentation</a>. It describes
27 how you can use <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> to create dynamically
28 configured virtual hosts.</p>
29
30 <div class="warning">mod_rewrite is not the best way to configure
31 virtual hosts. You should first consider the <a href="../vhosts/mass.html">alternatives</a> before resorting to
32 mod_rewrite. See also the "<a href="avoid.html#vhosts">how to avoid
33 mod_rewrite</a> document.</div>
34
35 </div>
36 <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#per-hostname">Virtual Hosts For Arbitrary Hostnames</a></li>
37 <li><img alt="" src="../images/down.gif" /> <a href="#simple.rewrite">Dynamic
38     Virtual Hosts Using <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code></a></li>
39 <li><img alt="" src="../images/down.gif" /> <a href="#xtra-conf">Using a Separate Virtual Host Configuration File</a></li>
40 </ul><h3>See also</h3><ul class="seealso"><li><a href="../mod/mod_rewrite.html">Module documentation</a></li><li><a href="intro.html">mod_rewrite introduction</a></li><li><a href="remapping.html">Redirection and remapping</a></li><li><a href="access.html">Controlling access</a></li><li><a href="proxy.html">Proxying</a></li><li><a href="rewritemap.html">RewriteMap</a></li><li><a href="advanced.html">Advanced techniques</a></li><li><a href="avoid.html">When not to use mod_rewrite</a></li></ul></div>
41 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
42 <div class="section">
43 <h2><a name="per-hostname" id="per-hostname">Virtual Hosts For Arbitrary Hostnames</a></h2>
44
45   
46
47   <dl>
48     <dt>Description:</dt>
49
50     <dd>
51     <p>We want to automatically create a virtual host for every hostname
52     which resolves in our domain, without having to create
53     new VirtualHost sections.</p>
54
55     <p>In this recipe, we assume that we'll be using the hostname
56     <code>www.<strong>SITE</strong>.example.com</code> for each
57     user, and serve their content out of
58     <code>/home/<strong>SITE</strong>/www</code>.</p>
59     </dd>
60
61     <dt>Solution:</dt>
62
63     <dd>
64
65 <div class="example"><p><code>
66 RewriteEngine on<br />
67 <br />
68 RewriteMap    lowercase int:tolower<br />
69 <br />
70 RewriteCond   %{lowercase:%{<strong>HTTP_HOST</strong>}}   ^www\.<strong>([^.]+)</strong>\.example\.com$<br />
71 RewriteRule   ^(.*) /home/<strong>%1</strong>/www$1
72 </code></p></div></dd>
73
74 <dt>Discussion</dt>
75     <dd>
76
77     <div class="warning">You will need to take care of the DNS
78     resolution - Apache does
79     not handle name resolution. You'll need either to create CNAME
80     records for each hostname, or a DNS wildcard record. Creating DNS
81     records is beyond the scope of this document.</div>
82
83 <p>The internal <code>tolower</code> RewriteMap directive is used to
84 ensure that the hostnames being used are all lowercase, so that there is
85 no ambiguity in the directory structure which must be created.</p>
86
87 <p>Parentheses used in a <code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code> are captured into the
88 backreferences <code>%1</code>, <code>%2</code>, etc, while parentheses
89 used in <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> are
90 captured into the backreferences <code>$1</code>, <code>$2</code>,
91 etc.</p>
92
93 <p>
94 As with many techniques discussed in this document, mod_rewrite really
95 isn't the best way to accomplish this task. You should, instead,
96 consider using <code class="module"><a href="../mod/mod_vhost_alias.html">mod_vhost_alias</a></code> instead, as it will much
97 more gracefully handle anything beyond serving static files, such as any
98 dynamic content, and Alias resolution.
99 </p>
100     </dd>
101   </dl>
102
103 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
104 <div class="section">
105 <h2><a name="simple.rewrite" id="simple.rewrite">Dynamic
106     Virtual Hosts Using <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code></a></h2>
107
108     <p>This extract from <code>httpd.conf</code> does the same
109     thing as <a href="#per-hostname">the first example</a>. The first
110     half is very similar to the corresponding part above, except for
111     some changes, required for backward compatibility and to make the
112     <code>mod_rewrite</code> part work properly; the second half
113     configures <code>mod_rewrite</code> to do the actual work.</p>
114
115     <p>Because <code>mod_rewrite</code> runs before other URI translation
116     modules (e.g., <code>mod_alias</code>), <code>mod_rewrite</code> must
117     be told to explicitly ignore any URLs that would have been handled
118     by those modules. And, because these rules would otherwise bypass
119     any <code>ScriptAlias</code> directives, we must have
120     <code>mod_rewrite</code> explicitly enact those mappings.</p>
121
122 <div class="example"><p><code>
123 # get the server name from the Host: header<br />
124 UseCanonicalName Off<br />
125 <br />
126 # splittable logs<br />
127 LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon<br />
128 CustomLog logs/access_log vcommon<br />
129 <br />
130 &lt;Directory /www/hosts&gt;<br />
131 <span class="indent">
132     # ExecCGI is needed here because we can't force<br />
133     # CGI execution in the way that ScriptAlias does<br />
134     Options FollowSymLinks ExecCGI<br />
135 </span>
136 &lt;/Directory&gt;<br />
137 <br />
138 RewriteEngine On<br />
139 <br />
140 # a ServerName derived from a Host: header may be any case at all<br />
141 RewriteMap  lowercase  int:tolower<br />
142 <br />
143 ## deal with normal documents first:<br />
144 # allow Alias /icons/ to work - repeat for other aliases<br />
145 RewriteCond  %{REQUEST_URI}  !^/icons/<br />
146 # allow CGIs to work<br />
147 RewriteCond  %{REQUEST_URI}  !^/cgi-bin/<br />
148 # do the magic<br />
149 RewriteRule  ^/(.*)$  /www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1<br />
150 <br />
151 ## and now deal with CGIs - we have to force a handler<br />
152 RewriteCond  %{REQUEST_URI}  ^/cgi-bin/<br />
153 RewriteRule  ^/(.*)$  /www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1  [H=cgi-script]<br />
154 </code></p></div>
155
156 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
157 <div class="section">
158 <h2><a name="xtra-conf" id="xtra-conf">Using a Separate Virtual Host Configuration File</a></h2>
159
160     <p>This arrangement uses more advanced <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
161     features to work out the translation from virtual host to document
162     root, from a separate configuration file. This provides more
163     flexibility, but requires more complicated configuration.</p>
164
165     <p>The <code>vhost.map</code> file should look something like
166     this:</p>
167
168 <div class="example"><p><code>
169 customer-1.example.com  /www/customers/1<br />
170 customer-2.example.com  /www/customers/2<br />
171 # ...<br />
172 customer-N.example.com  /www/customers/N<br />
173 </code></p></div>
174
175     <p>The <code>httpd.conf</code> should contain the following:</p>
176
177 <div class="example"><p><code>
178 RewriteEngine on<br />
179 <br />
180 RewriteMap   lowercase  int:tolower<br />
181 <br />
182 # define the map file<br />
183 RewriteMap   vhost      txt:/www/conf/vhost.map<br />
184 <br />
185 # deal with aliases as above<br />
186 RewriteCond  %{REQUEST_URI}               !^/icons/<br />
187 RewriteCond  %{REQUEST_URI}               !^/cgi-bin/<br />
188 RewriteCond  ${lowercase:%{SERVER_NAME}}  ^(.+)$<br />
189 # this does the file-based remap<br />
190 RewriteCond  ${vhost:%1}                  ^(/.*)$<br />
191 RewriteRule  ^/(.*)$                      %1/docs/$1<br />
192 <br />
193 RewriteCond  %{REQUEST_URI}               ^/cgi-bin/<br />
194 RewriteCond  ${lowercase:%{SERVER_NAME}}  ^(.+)$<br />
195 RewriteCond  ${vhost:%1}                  ^(/.*)$<br />
196 RewriteRule  ^/(.*)$                      %1/cgi-bin/$1 [H=cgi-script]
197 </code></p></div>
198
199 </div></div>
200 <div class="bottomlang">
201 <p><span>Available Languages: </span><a href="../en/rewrite/vhosts.html" title="English">&nbsp;en&nbsp;</a></p>
202 </div><div id="footer">
203 <p class="apache">Copyright 2012 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
204 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div>
205 </body></html>