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
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" /><link rel="stylesheet" type="text/css" href="../style/css/prettify.css" />
12 <script src="../style/scripts/prettify.js" type="text/javascript">
15 <link href="../images/favicon.ico" rel="shortcut icon" /></head>
16 <body id="manual-page"><div id="page-header">
17 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
18 <p class="apache">Apache HTTP Server Version 2.5</p>
19 <img alt="" src="../images/feather.gif" /></div>
20 <div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
22 <a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.5</a> > <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>Dynamic mass virtual hosts with mod_rewrite</h1>
24 <p><span>Available Languages: </span><a href="../en/rewrite/vhosts.html" title="English"> en </a></p>
28 <p>This document supplements the <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
29 <a href="../mod/mod_rewrite.html">reference documentation</a>. It describes
30 how you can use <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> to create dynamically
31 configured virtual hosts.</p>
33 <div class="warning">mod_rewrite is not the best way to configure
34 virtual hosts. You should first consider the <a href="../vhosts/mass.html">alternatives</a> before resorting to
35 mod_rewrite. See also the "<a href="avoid.html#vhosts">how to avoid
36 mod_rewrite</a> document.</div>
39 <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#per-hostname">Virtual Hosts For Arbitrary Hostnames</a></li>
40 <li><img alt="" src="../images/down.gif" /> <a href="#simple.rewrite">Dynamic
41 Virtual Hosts Using <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code></a></li>
42 <li><img alt="" src="../images/down.gif" /> <a href="#xtra-conf">Using a Separate Virtual Host Configuration File</a></li>
43 </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><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div>
44 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
46 <h2><a name="per-hostname" id="per-hostname">Virtual Hosts For Arbitrary Hostnames</a></h2>
54 <p>We want to automatically create a virtual host for every hostname
55 which resolves in our domain, without having to create
56 new VirtualHost sections.</p>
58 <p>In this recipe, we assume that we'll be using the hostname
59 <code>www.<strong>SITE</strong>.example.com</code> for each
60 user, and serve their content out of
61 <code>/home/<strong>SITE</strong>/www</code>.</p>
68 <pre class="prettyprint lang-config">
71 RewriteMap lowercase int:tolower
73 RewriteCond ${lowercase:%{<strong>HTTP_HOST</strong>}} ^www\.<strong>([^.]+)</strong>\.example\.com$
74 RewriteRule ^(.*) /home/<strong>%1</strong>/www$1
81 <div class="warning">You will need to take care of the DNS
82 resolution - Apache does
83 not handle name resolution. You'll need either to create CNAME
84 records for each hostname, or a DNS wildcard record. Creating DNS
85 records is beyond the scope of this document.</div>
87 <p>The internal <code>tolower</code> RewriteMap directive is used to
88 ensure that the hostnames being used are all lowercase, so that there is
89 no ambiguity in the directory structure which must be created.</p>
91 <p>Parentheses used in a <code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code> are captured into the
92 backreferences <code>%1</code>, <code>%2</code>, etc, while parentheses
93 used in <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> are
94 captured into the backreferences <code>$1</code>, <code>$2</code>,
98 As with many techniques discussed in this document, mod_rewrite really
99 isn't the best way to accomplish this task. You should, instead,
100 consider using <code class="module"><a href="../mod/mod_vhost_alias.html">mod_vhost_alias</a></code> instead, as it will much
101 more gracefully handle anything beyond serving static files, such as any
102 dynamic content, and Alias resolution.
107 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
108 <div class="section">
109 <h2><a name="simple.rewrite" id="simple.rewrite">Dynamic
110 Virtual Hosts Using <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code></a></h2>
112 <p>This extract from <code>httpd.conf</code> does the same
113 thing as <a href="#per-hostname">the first example</a>. The first
114 half is very similar to the corresponding part above, except for
115 some changes, required for backward compatibility and to make the
116 <code>mod_rewrite</code> part work properly; the second half
117 configures <code>mod_rewrite</code> to do the actual work.</p>
119 <p>Because <code>mod_rewrite</code> runs before other URI translation
120 modules (e.g., <code>mod_alias</code>), <code>mod_rewrite</code> must
121 be told to explicitly ignore any URLs that would have been handled
122 by those modules. And, because these rules would otherwise bypass
123 any <code>ScriptAlias</code> directives, we must have
124 <code>mod_rewrite</code> explicitly enact those mappings.</p>
126 <pre class="prettyprint lang-config">
127 # get the server name from the Host: header
131 LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
132 CustomLog logs/access_log vcommon
134 <Directory /www/hosts>
135 # ExecCGI is needed here because we can't force
136 # CGI execution in the way that ScriptAlias does
137 Options FollowSymLinks ExecCGI
142 # a ServerName derived from a Host: header may be any case at all
143 RewriteMap lowercase int:tolower
145 ## deal with normal documents first:
146 # allow Alias /icons/ to work - repeat for other aliases
147 RewriteCond %{REQUEST_URI} !^/icons/
149 RewriteCond %{REQUEST_URI} !^/cgi-bin/
151 RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1
153 ## and now deal with CGIs - we have to force a handler
154 RewriteCond %{REQUEST_URI} ^/cgi-bin/
155 RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1 [H=cgi-script]
159 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
160 <div class="section">
161 <h2><a name="xtra-conf" id="xtra-conf">Using a Separate Virtual Host Configuration File</a></h2>
163 <p>This arrangement uses more advanced <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
164 features to work out the translation from virtual host to document
165 root, from a separate configuration file. This provides more
166 flexibility, but requires more complicated configuration.</p>
168 <p>The <code>vhost.map</code> file should look something like
171 <div class="example"><p><code>
172 customer-1.example.com /www/customers/1<br />
173 customer-2.example.com /www/customers/2<br />
175 customer-N.example.com /www/customers/N<br />
178 <p>The <code>httpd.conf</code> should contain the following:</p>
180 <pre class="prettyprint lang-config">
183 RewriteMap lowercase int:tolower
185 # define the map file
186 RewriteMap vhost txt:/www/conf/vhost.map
188 # deal with aliases as above
189 RewriteCond %{REQUEST_URI} !^/icons/
190 RewriteCond %{REQUEST_URI} !^/cgi-bin/
191 RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
192 # this does the file-based remap
193 RewriteCond ${vhost:%1} ^(/.*)$
194 RewriteRule ^/(.*)$ %1/docs/$1
196 RewriteCond %{REQUEST_URI} ^/cgi-bin/
197 RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
198 RewriteCond ${vhost:%1} ^(/.*)$
199 RewriteRule ^/(.*)$ %1/cgi-bin/$1 [H=cgi-script]
204 <div class="bottomlang">
205 <p><span>Available Languages: </span><a href="../en/rewrite/vhosts.html" title="English"> en </a></p>
206 </div><div class="top"><a href="#page-header"><img src="../images/up.gif" alt="top" /></a></div><div class="section"><h2><a id="comments_section" name="comments_section">Comments</a></h2><div class="warning"><strong>This section is experimental!</strong><br />Comments placed here should not be expected
207 to last beyond the testing phase of this system, nor do we in any way guarantee that we'll read them.</div>
208 <script type="text/javascript"><!--//--><![CDATA[//><!--
209 var disqus_shortname = 'httpd';
210 var disqus_identifier = 'http://httpd.apache.org/docs/2.4/rewrite/vhosts.html.en';
212 if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
213 d.write('<div id="disqus_thread"><\/div>');
214 var s = d.createElement('script');
215 s.type = 'text/javascript';
217 s.src = 'http' + '://' + disqus_shortname + '.disqus.com/embed.js';
218 (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
221 d.write('<div id="disqus_thread">Comments have been disabled for offline viewing.<\/div>');
223 })(window, document);
224 //--><!]]></script></div><script type="text/javascript"><!--//--><![CDATA[//><!--
226 var _gaq = _gaq || [];
227 _gaq.push(['_setAccount', 'UA-31727287-1']);
228 _gaq.push(['_trackPageview']);
229 if (window.location.hostname.toLowerCase() == "httpd.apache.org") {
231 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
232 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
233 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
236 //--><!]]></script><div id="footer">
237 <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>
238 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!--
239 if (typeof(prettyPrint) !== 'undefined') {