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" /><link rel="stylesheet" type="text/css" href="/style/css/prettify.css" /> 12<script src="/style/scripts/prettify.min.js" type="text/javascript"> 13</script> 14 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.4</p> 19<img alt="" src="/images/feather.gif" /></div> 20<div class="up"><a href="./"><img title="<-" alt="<-" src="/images/left.gif" /></a></div> 21<div id="path"> 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.4</a> > <a href="./">Rewrite</a></div><div id="page-content"><div id="preamble"><h1>Dynamic mass virtual hosts with mod_rewrite</h1> 23<div class="toplang"> 24<p><span>Available Languages: </span><a href="/en/rewrite/vhosts.html" title="English"> en </a> | 25<a href="/fr/rewrite/vhosts.html" hreflang="fr" rel="alternate" title="Fran�ais"> fr </a></p> 26</div> 27 28 29<p>This document supplements the <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code> 30<a href="/mod/mod_rewrite.html">reference documentation</a>. It describes 31how you can use <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code> to create dynamically 32configured virtual hosts.</p> 33 34<div class="warning">mod_rewrite is not the best way to configure 35virtual hosts. You should first consider the <a href="/vhosts/mass.html">alternatives</a> before resorting to 36mod_rewrite. See also the "<a href="avoid.html#vhosts">how to avoid 37mod_rewrite</a> document.</div> 38 39</div> 40<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#per-hostname">Virtual Hosts For Arbitrary Hostnames</a></li> 41<li><img alt="" src="/images/down.gif" /> <a href="#simple.rewrite">Dynamic 42 Virtual Hosts Using <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code></a></li> 43<li><img alt="" src="/images/down.gif" /> <a href="#xtra-conf">Using a Separate Virtual Host Configuration File</a></li> 44</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> 45<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div> 46<div class="section"> 47<h2><a name="per-hostname" id="per-hostname">Virtual Hosts For Arbitrary Hostnames</a></h2> 48 49 50 51 <dl> 52 <dt>Description:</dt> 53 54 <dd> 55 <p>We want to automatically create a virtual host for every hostname 56 which resolves in our domain, without having to create 57 new VirtualHost sections.</p> 58 59 <p>In this recipe, we assume that we'll be using the hostname 60 <code>www.<strong>SITE</strong>.example.com</code> for each 61 user, and serve their content out of 62 <code>/home/<strong>SITE</strong>/www</code>.</p> 63 </dd> 64 65 <dt>Solution:</dt> 66 67 <dd> 68 69<pre class="prettyprint lang-config">RewriteEngine on 70 71RewriteMap lowercase int:tolower 72 73RewriteCond ${lowercase:%{<strong>HTTP_HOST</strong>}} ^www\.<strong>([^.]+)</strong>\.example\.com$ 74RewriteRule ^(.*) /home/<strong>%1</strong>/www$1</pre> 75</dd> 76 77<dt>Discussion</dt> 78 <dd> 79 80 <div class="warning">You will need to take care of the DNS 81 resolution - Apache does 82 not handle name resolution. You'll need either to create CNAME 83 records for each hostname, or a DNS wildcard record. Creating DNS 84 records is beyond the scope of this document.</div> 85 86<p>The internal <code>tolower</code> RewriteMap directive is used to 87ensure that the hostnames being used are all lowercase, so that there is 88no ambiguity in the directory structure which must be created.</p> 89 90<p>Parentheses used in a <code class="directive"><a href="/mod/mod_rewrite.html#rewritecond">RewriteCond</a></code> are captured into the 91backreferences <code>%1</code>, <code>%2</code>, etc, while parentheses 92used in <code class="directive"><a href="/mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> are 93captured into the backreferences <code>$1</code>, <code>$2</code>, 94etc.</p> 95 96<p> 97As with many techniques discussed in this document, mod_rewrite really 98isn't the best way to accomplish this task. You should, instead, 99consider using <code class="module"><a href="/mod/mod_vhost_alias.html">mod_vhost_alias</a></code> instead, as it will much 100more gracefully handle anything beyond serving static files, such as any 101dynamic content, and Alias resolution. 102</p> 103 </dd> 104 </dl> 105 106</div><div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div> 107<div class="section"> 108<h2><a name="simple.rewrite" id="simple.rewrite">Dynamic 109 Virtual Hosts Using <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code></a></h2> 110 111 <p>This extract from <code>httpd.conf</code> does the same 112 thing as <a href="#per-hostname">the first example</a>. The first 113 half is very similar to the corresponding part above, except for 114 some changes, required for backward compatibility and to make the 115 <code>mod_rewrite</code> part work properly; the second half 116 configures <code>mod_rewrite</code> to do the actual work.</p> 117 118 <p>Because <code>mod_rewrite</code> runs before other URI translation 119 modules (e.g., <code>mod_alias</code>), <code>mod_rewrite</code> must 120 be told to explicitly ignore any URLs that would have been handled 121 by those modules. And, because these rules would otherwise bypass 122 any <code>ScriptAlias</code> directives, we must have 123 <code>mod_rewrite</code> explicitly enact those mappings.</p> 124 125<pre class="prettyprint lang-config"># get the server name from the Host: header 126UseCanonicalName Off 127 128# splittable logs 129LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon 130CustomLog logs/access_log vcommon 131 132<Directory /www/hosts> 133 # ExecCGI is needed here because we can't force 134 # CGI execution in the way that ScriptAlias does 135 Options FollowSymLinks ExecCGI 136</Directory> 137 138RewriteEngine On 139 140# a ServerName derived from a Host: header may be any case at all 141RewriteMap lowercase int:tolower 142 143## deal with normal documents first: 144# allow Alias /icons/ to work - repeat for other aliases 145RewriteCond %{REQUEST_URI} !^/icons/ 146# allow CGIs to work 147RewriteCond %{REQUEST_URI} !^/cgi-bin/ 148# do the magic 149RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1 150 151## and now deal with CGIs - we have to force a handler 152RewriteCond %{REQUEST_URI} ^/cgi-bin/ 153RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1 [H=cgi-script]</pre> 154 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> 169customer-1.example.com /www/customers/1<br /> 170customer-2.example.com /www/customers/2<br /> 171# ...<br /> 172customer-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<pre class="prettyprint lang-config">RewriteEngine on 178 179RewriteMap lowercase int:tolower 180 181# define the map file 182RewriteMap vhost txt:/www/conf/vhost.map 183 184# deal with aliases as above 185RewriteCond %{REQUEST_URI} !^/icons/ 186RewriteCond %{REQUEST_URI} !^/cgi-bin/ 187RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$ 188# this does the file-based remap 189RewriteCond ${vhost:%1} ^(/.*)$ 190RewriteRule ^/(.*)$ %1/docs/$1 191 192RewriteCond %{REQUEST_URI} ^/cgi-bin/ 193RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$ 194RewriteCond ${vhost:%1} ^(/.*)$ 195RewriteRule ^/(.*)$ %1/cgi-bin/$1 [H=cgi-script]</pre> 196 197 198</div></div> 199<div class="bottomlang"> 200<p><span>Available Languages: </span><a href="/en/rewrite/vhosts.html" title="English"> en </a> | 201<a href="/fr/rewrite/vhosts.html" hreflang="fr" rel="alternate" title="Fran�ais"> fr </a></p> 202</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> 203<script type="text/javascript"><!--//--><![CDATA[//><!-- 204var comments_shortname = 'httpd'; 205var comments_identifier = 'http://httpd.apache.org/docs/2.4/rewrite/vhosts.html'; 206(function(w, d) { 207 if (w.location.hostname.toLowerCase() == "httpd.apache.org") { 208 d.write('<div id="comments_thread"><\/div>'); 209 var s = d.createElement('script'); 210 s.type = 'text/javascript'; 211 s.async = true; 212 s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier; 213 (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s); 214 } 215 else { 216 d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>'); 217 } 218})(window, document); 219//--><!]]></script></div><div id="footer"> 220<p class="apache">Copyright 2014 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> 221<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[//><!-- 222if (typeof(prettyPrint) !== 'undefined') { 223 prettyPrint(); 224} 225//--><!]]></script> 226</body></html>