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 <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type" />
6 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7 This file is generated from xml source: DO NOT EDIT
8 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
10 <title>Dynamic mass virtual hosts with mod_rewrite - Apache HTTP Server Version 2.5</title>
11 <link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
12 <link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
13 <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" />
14 <script src="../style/scripts/prettify.min.js" type="text/javascript">
17 <link href="../images/favicon.ico" rel="shortcut icon" /></head>
18 <body id="manual-page"><div id="page-header">
19 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.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>
20 <p class="apache">Apache HTTP Server Version 2.5</p>
21 <img alt="" src="../images/feather.png" /></div>
22 <div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
24 <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>
26 <p><span>Available Languages: </span><a href="../en/rewrite/vhosts.html" title="English"> en </a></p>
30 <p>This document supplements the <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
31 <a href="../mod/mod_rewrite.html">reference documentation</a>. It describes
32 how you can use <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code> to create dynamically
33 configured virtual hosts.</p>
35 <div class="warning">mod_rewrite is usually not the best way to configure
36 virtual hosts. You should first consider the <a href="../vhosts/mass.html">alternatives</a> before resorting to
37 mod_rewrite. See also the "<a href="avoid.html#vhosts">how to avoid
38 mod_rewrite</a> document.</div>
41 <div id="quickview"><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#per-hostname">Virtual Hosts For Arbitrary Hostnames</a></li>
42 <li><img alt="" src="../images/down.gif" /> <a href="#simple.rewrite">Dynamic
43 Virtual Hosts Using <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code></a></li>
44 <li><img alt="" src="../images/down.gif" /> <a href="#xtra-conf">Using a Separate Virtual Host Configuration File</a></li>
45 </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><li><a href="#comments_section">Comments</a></li></ul></div>
46 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
48 <h2><a name="per-hostname" id="per-hostname">Virtual Hosts For Arbitrary Hostnames</a></h2>
56 <p>We want to automatically create a virtual host for every hostname
57 which resolves in our domain, without having to create
58 new VirtualHost sections.</p>
60 <p>In this recipe, we assume that we'll be using the hostname
61 <code><strong>SITE</strong>.example.com</code> for each
62 user, and serve their content out of
63 <code>/home/<strong>SITE</strong>/www</code>. However, we want
64 <code>www.example.com</code> to be ommitted from this mapping.</p>
71 <pre class="prettyprint lang-config">RewriteEngine on
73 RewriteMap lowercase int:tolower
75 RewriteCond %{HTTP_HOST} !^www\.
76 RewriteCond ${lowercase:%{<strong>HTTP_HOST</strong>}} ^<strong>([^.]+)</strong>\.example\.com$
77 RewriteRule ^(.*) /home/<strong>%1</strong>/www$1</pre>
83 <div class="warning">You will need to take care of the DNS
84 resolution - Apache does
85 not handle name resolution. You'll need either to create CNAME
86 records for each hostname, or a DNS wildcard record. Creating DNS
87 records is beyond the scope of this document.</div>
89 <p>The internal <code>tolower</code> RewriteMap directive is used to
90 ensure that the hostnames being used are all lowercase, so that there is
91 no ambiguity in the directory structure which must be created.</p>
93 <p>Parentheses used in a <code class="directive"><a href="../mod/mod_rewrite.html#rewritecond">RewriteCond</a></code> are captured into the
94 backreferences <code>%1</code>, <code>%2</code>, etc, while parentheses
95 used in <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> are
96 captured into the backreferences <code>$1</code>, <code>$2</code>,
99 <p>The first <code>RewriteCond</code> checks to see if the hostname
100 starts with <code>www.</code>, and if it does, the rewriting is
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 <code class="module"><a href="../mod/mod_vhost_alias.html">mod_vhost_alias</a></code> instead, as it will much
107 more gracefully handle anything beyond serving static files, such as any
108 dynamic content, and Alias resolution.
113 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
114 <div class="section">
115 <h2><a name="simple.rewrite" id="simple.rewrite">Dynamic
116 Virtual Hosts Using <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code></a></h2>
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>
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>
132 <pre class="prettyprint lang-config"># get the server name from the Host: header
136 LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
137 CustomLog "logs/access_log" vcommon
139 <Directory "/www/hosts">
140 # ExecCGI is needed here because we can't force
141 # CGI execution in the way that ScriptAlias does
142 Options FollowSymLinks ExecCGI
147 # a ServerName derived from a Host: header may be any case at all
148 RewriteMap lowercase "int:tolower"
150 ## deal with normal documents first:
151 # allow Alias /icons/ to work - repeat for other aliases
152 RewriteCond "%{REQUEST_URI}" "!^/icons/"
154 RewriteCond "%{REQUEST_URI}" "!^/cgi-bin/"
156 RewriteRule "^/(.*)$" "/www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1"
158 ## and now deal with CGIs - we have to force a handler
159 RewriteCond "%{REQUEST_URI}" "^/cgi-bin/"
160 RewriteRule "^/(.*)$" "/www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1" [H=cgi-script]</pre>
163 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
164 <div class="section">
165 <h2><a name="xtra-conf" id="xtra-conf">Using a Separate Virtual Host Configuration File</a></h2>
167 <p>This arrangement uses more advanced <code class="module"><a href="../mod/mod_rewrite.html">mod_rewrite</a></code>
168 features to work out the translation from virtual host to document
169 root, from a separate configuration file. This provides more
170 flexibility, but requires more complicated configuration.</p>
172 <p>The <code>vhost.map</code> file should look something like
175 <div class="example"><p><code>
176 customer-1.example.com /www/customers/1<br />
177 customer-2.example.com /www/customers/2<br />
179 customer-N.example.com /www/customers/N<br />
182 <p>The <code>httpd.conf</code> should contain the following:</p>
184 <pre class="prettyprint lang-config">RewriteEngine on
186 RewriteMap lowercase "int:tolower"
188 # define the map file
189 RewriteMap vhost "txt:/www/conf/vhost.map"
191 # deal with aliases as above
192 RewriteCond "%{REQUEST_URI}" "!^/icons/"
193 RewriteCond "%{REQUEST_URI}" "!^/cgi-bin/"
194 RewriteCond "${lowercase:%{SERVER_NAME}}" "^(.+)$"
195 # this does the file-based remap
196 RewriteCond "${vhost:%1}" "^(/.*)$"
197 RewriteRule "^/(.*)$" "%1/docs/$1"
199 RewriteCond "%{REQUEST_URI}" "^/cgi-bin/"
200 RewriteCond "${lowercase:%{SERVER_NAME}}" "^(.+)$"
201 RewriteCond "${vhost:%1}" "^(/.*)$"
202 RewriteRule "^/cgi-bin/(.*)$" "%1/cgi-bin/$1" [H=cgi-script]</pre>
206 <div class="bottomlang">
207 <p><span>Available Languages: </span><a href="../en/rewrite/vhosts.html" title="English"> en </a></p>
208 </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>Notice:</strong><br />This is not a Q&A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our <a href="http://httpd.apache.org/lists.html">mailing lists</a>.</div>
209 <script type="text/javascript"><!--//--><![CDATA[//><!--
210 var comments_shortname = 'httpd';
211 var comments_identifier = 'http://httpd.apache.org/docs/trunk/rewrite/vhosts.html';
213 if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
214 d.write('<div id="comments_thread"><\/div>');
215 var s = d.createElement('script');
216 s.type = 'text/javascript';
218 s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
219 (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
222 d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
224 })(window, document);
225 //--><!]]></script></div><div id="footer">
226 <p class="apache">Copyright 2016 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>
227 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.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[//><!--
228 if (typeof(prettyPrint) !== 'undefined') {