vhosts.html.en revision 50d324d24e6146c9e21573a0857d9d850190dad6
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
This file is generated from xml source: DO NOT EDIT
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
-->
<title>Dynamic mass virtual hosts with mod_rewrite - Apache HTTP Server</title>
<link href="/style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
<link href="/style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
<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" />
</script>
<body id="manual-page"><div id="page-header">
<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>
<p class="apache">Apache HTTP Server Version 2.5</p>
<div id="path">
<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>
<div class="toplang">
<p><span>Available Languages: </span><a href="/en/rewrite/vhosts.html" title="English"> en </a></p>
</div>
<p>This document supplements the <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>
how you can use <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code> to create dynamically
configured virtual hosts.</p>
<div class="warning">mod_rewrite is not the best way to configure
virtual hosts. You should first consider the <a href="/vhosts/mass.html">alternatives</a> before resorting to
mod_rewrite. See also the "<a href="avoid.html#vhosts">how to avoid
mod_rewrite</a> document.</div>
</div>
<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#per-hostname">Virtual Hosts For Arbitrary Hostnames</a></li>
Virtual Hosts Using <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code></a></li>
<li><img alt="" src="/images/down.gif" /> <a href="#xtra-conf">Using a Separate Virtual Host Configuration File</a></li>
</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>
<div class="section">
<h2><a name="per-hostname" id="per-hostname">Virtual Hosts For Arbitrary Hostnames</a></h2>
<dl>
<dt>Description:</dt>
<dd>
<p>We want to automatically create a virtual host for every hostname
which resolves in our domain, without having to create
new VirtualHost sections.</p>
<p>In this recipe, we assume that we'll be using the hostname
user, and serve their content out of
<code>/home/<strong>SITE</strong>/www</code>.</p>
</dd>
<dt>Solution:</dt>
<dd>
<pre class="prettyprint lang-config">
RewriteEngine on
RewriteMap lowercase int:tolower
RewriteCond %{lowercase:%{<strong>HTTP_HOST</strong>}} ^www\.<strong>([^.]+)</strong>\.example\.com$
RewriteRule ^(.*) /home/<strong>%1</strong>/www$1
</pre>
</dd>
<dt>Discussion</dt>
<dd>
<div class="warning">You will need to take care of the DNS
resolution - Apache does
not handle name resolution. You'll need either to create CNAME
records for each hostname, or a DNS wildcard record. Creating DNS
records is beyond the scope of this document.</div>
<p>The internal <code>tolower</code> RewriteMap directive is used to
ensure that the hostnames being used are all lowercase, so that there is
no ambiguity in the directory structure which must be created.</p>
<p>Parentheses used in a <code class="directive"><a href="/mod/mod_rewrite.html#rewritecond">RewriteCond</a></code> are captured into the
backreferences <code>%1</code>, <code>%2</code>, etc, while parentheses
used in <code class="directive"><a href="/mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> are
captured into the backreferences <code>$1</code>, <code>$2</code>,
etc.</p>
<p>
As with many techniques discussed in this document, mod_rewrite really
isn't the best way to accomplish this task. You should, instead,
consider using <code class="module"><a href="/mod/mod_vhost_alias.html">mod_vhost_alias</a></code> instead, as it will much
more gracefully handle anything beyond serving static files, such as any
dynamic content, and Alias resolution.
</p>
</dd>
</dl>
<div class="section">
Virtual Hosts Using <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code></a></h2>
thing as <a href="#per-hostname">the first example</a>. The first
half is very similar to the corresponding part above, except for
some changes, required for backward compatibility and to make the
<code>mod_rewrite</code> part work properly; the second half
configures <code>mod_rewrite</code> to do the actual work.</p>
<p>Because <code>mod_rewrite</code> runs before other URI translation
be told to explicitly ignore any URLs that would have been handled
by those modules. And, because these rules would otherwise bypass
any <code>ScriptAlias</code> directives, we must have
<code>mod_rewrite</code> explicitly enact those mappings.</p>
<pre class="prettyprint lang-config">
# get the server name from the Host: header
UseCanonicalName Off
# splittable logs
LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/access_log vcommon
# ExecCGI is needed here because we can't force
# CGI execution in the way that ScriptAlias does
Options FollowSymLinks ExecCGI
</Directory>
RewriteEngine On
# a ServerName derived from a Host: header may be any case at all
RewriteMap lowercase int:tolower
## deal with normal documents first:
# allow Alias /icons/ to work - repeat for other aliases
RewriteCond %{REQUEST_URI} !^/icons/
# allow CGIs to work
RewriteCond %{REQUEST_URI} !^/cgi-bin/
# do the magic
## and now deal with CGIs - we have to force a handler
RewriteCond %{REQUEST_URI} ^/cgi-bin/
</pre>
<div class="section">
<h2><a name="xtra-conf" id="xtra-conf">Using a Separate Virtual Host Configuration File</a></h2>
<p>This arrangement uses more advanced <code class="module"><a href="/mod/mod_rewrite.html">mod_rewrite</a></code>
features to work out the translation from virtual host to document
root, from a separate configuration file. This provides more
flexibility, but requires more complicated configuration.</p>
this:</p>
<div class="example"><p><code>
# ...<br />
</code></p></div>
<pre class="prettyprint lang-config">
RewriteEngine on
RewriteMap lowercase int:tolower
# define the map file
# deal with aliases as above
RewriteCond %{REQUEST_URI} !^/icons/
RewriteCond %{REQUEST_URI} !^/cgi-bin/
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
# this does the file-based remap
RewriteCond ${vhost:%1} ^(/.*)$
RewriteCond %{REQUEST_URI} ^/cgi-bin/
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${vhost:%1} ^(/.*)$
</pre>
</div></div>
<div class="bottomlang">
<p><span>Available Languages: </span><a href="/en/rewrite/vhosts.html" title="English"> en </a></p>
</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
to last beyond the testing phase of this system, nor do we in any way guarantee that we'll read them.</div><div id="disqus_thread" /><script type="text/javascript"><!--//--><![CDATA[//><!--
var lang = 'en';
var disqus_shortname = 'httpd';
var disqus_identifier = window.location.href.replace(/(current|trunk)/, "2.4").replace(/\/[a-z]{2}\//, "/") + '.' + lang;
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
//--><!]]></script></div><div id="footer">
<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>
<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[//><!--
if (typeof(prettyPrint) !== undefined) {
prettyPrint();
}
//--><!]]></script>
</body></html>