10705fbc225dcf590c9ba24a4f04de221358060arbowen<?xml version="1.0" encoding="UTF-8" ?>
10705fbc225dcf590c9ba24a4f04de221358060arbowen<!DOCTYPE manualpage SYSTEM "/style/manualpage.dtd">
10705fbc225dcf590c9ba24a4f04de221358060arbowen<?xml-stylesheet type="text/xsl" href="/style/manual.en.xsl"?>
6896b3498e144d33f374efe93e6bab2669af535fnd<!-- $LastChangedRevision$ -->
10705fbc225dcf590c9ba24a4f04de221358060arbowen
10705fbc225dcf590c9ba24a4f04de221358060arbowen<!--
10705fbc225dcf590c9ba24a4f04de221358060arbowen Licensed to the Apache Software Foundation (ASF) under one or more
10705fbc225dcf590c9ba24a4f04de221358060arbowen contributor license agreements. See the NOTICE file distributed with
10705fbc225dcf590c9ba24a4f04de221358060arbowen this work for additional information regarding copyright ownership.
10705fbc225dcf590c9ba24a4f04de221358060arbowen The ASF licenses this file to You under the Apache License, Version 2.0
10705fbc225dcf590c9ba24a4f04de221358060arbowen (the "License"); you may not use this file except in compliance with
10705fbc225dcf590c9ba24a4f04de221358060arbowen the License. You may obtain a copy of the License at
10705fbc225dcf590c9ba24a4f04de221358060arbowen
10705fbc225dcf590c9ba24a4f04de221358060arbowen http://www.apache.org/licenses/LICENSE-2.0
10705fbc225dcf590c9ba24a4f04de221358060arbowen
10705fbc225dcf590c9ba24a4f04de221358060arbowen Unless required by applicable law or agreed to in writing, software
10705fbc225dcf590c9ba24a4f04de221358060arbowen distributed under the License is distributed on an "AS IS" BASIS,
10705fbc225dcf590c9ba24a4f04de221358060arbowen WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10705fbc225dcf590c9ba24a4f04de221358060arbowen See the License for the specific language governing permissions and
10705fbc225dcf590c9ba24a4f04de221358060arbowen limitations under the License.
10705fbc225dcf590c9ba24a4f04de221358060arbowen-->
10705fbc225dcf590c9ba24a4f04de221358060arbowen
10705fbc225dcf590c9ba24a4f04de221358060arbowen<manualpage metafile="vhosts.xml.meta">
10705fbc225dcf590c9ba24a4f04de221358060arbowen <parentdocument href="./">Rewrite</parentdocument>
10705fbc225dcf590c9ba24a4f04de221358060arbowen
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen<title>Dynamic mass virtual hosts with mod_rewrite</title>
10705fbc225dcf590c9ba24a4f04de221358060arbowen
10705fbc225dcf590c9ba24a4f04de221358060arbowen<summary>
10705fbc225dcf590c9ba24a4f04de221358060arbowen
860b4efe27e7c1c9a2bf5c872b29c90f76849b51jim<p>This document supplements the <module>mod_rewrite</module>
10705fbc225dcf590c9ba24a4f04de221358060arbowen<a href="/mod/mod_rewrite.html">reference documentation</a>. It describes
860b4efe27e7c1c9a2bf5c872b29c90f76849b51jimhow you can use <module>mod_rewrite</module> to create dynamically
10705fbc225dcf590c9ba24a4f04de221358060arbowenconfigured virtual hosts.</p>
10705fbc225dcf590c9ba24a4f04de221358060arbowen
10705fbc225dcf590c9ba24a4f04de221358060arbowen<note type="warning">mod_rewrite is not the best way to configure
10705fbc225dcf590c9ba24a4f04de221358060arbowenvirtual hosts. You should first consider the <a
10705fbc225dcf590c9ba24a4f04de221358060arbowenhref="/vhosts/mass.html">alternatives</a> before resorting to
369082e032313641ad20a7c588d0d0922db6aaa9rbowenmod_rewrite. See also the "<a href="avoid.html#vhosts">how to avoid
369082e032313641ad20a7c588d0d0922db6aaa9rbowenmod_rewrite</a> document.</note>
10705fbc225dcf590c9ba24a4f04de221358060arbowen
10705fbc225dcf590c9ba24a4f04de221358060arbowen</summary>
10705fbc225dcf590c9ba24a4f04de221358060arbowen<seealso><a href="/mod/mod_rewrite.html">Module documentation</a></seealso>
10705fbc225dcf590c9ba24a4f04de221358060arbowen<seealso><a href="intro.html">mod_rewrite introduction</a></seealso>
10705fbc225dcf590c9ba24a4f04de221358060arbowen<seealso><a href="remapping.html">Redirection and remapping</a></seealso>
10705fbc225dcf590c9ba24a4f04de221358060arbowen<seealso><a href="access.html">Controlling access</a></seealso>
10705fbc225dcf590c9ba24a4f04de221358060arbowen<!--<seealso><a href="vhosts.html">Virtual hosts</a></seealso>-->
10705fbc225dcf590c9ba24a4f04de221358060arbowen<seealso><a href="proxy.html">Proxying</a></seealso>
365711300b7310a1efed8aa9f01492086882ba7crbowen<seealso><a href="rewritemap.html">RewriteMap</a></seealso>
53abc235688d883cfa15cdfec354ba03128f357arbowen<seealso><a href="advanced.html">Advanced techniques</a></seealso>
10705fbc225dcf590c9ba24a4f04de221358060arbowen<seealso><a href="avoid.html">When not to use mod_rewrite</a></seealso>
10705fbc225dcf590c9ba24a4f04de221358060arbowen
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen<section id="per-hostname">
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen
7aae1084ea6db741a017571545f5be0914a03148lgentis <title>Virtual Hosts For Arbitrary Hostnames</title>
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen <dl>
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen <dt>Description:</dt>
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen <dd>
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen <p>We want to automatically create a virtual host for every hostname
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen which resolves in our domain, without having to create
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen new VirtualHost sections.</p>
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen <p>In this recipe, we assume that we'll be using the hostname
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen <code>www.<strong>SITE</strong>.example.com</code> for each
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen user, and serve their content out of
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen <code>/home/<strong>SITE</strong>/www</code>.</p>
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen </dd>
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen <dt>Solution:</dt>
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen <dd>
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh<highlight language="config">
0a0fd048f54df3e9053942466292da579524c1fdhumbedoohRewriteEngine on
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteMap lowercase "int:tolower"
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteCond "${lowercase:%{<strong>HTTP_HOST</strong>}}" "^www\.<strong>([^.]+)</strong>\.example\.com$"
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteRule "^(.*)" "/home/<strong>%1</strong>/www$1"
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh</highlight></dd>
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen<dt>Discussion</dt>
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen <dd>
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen
860b4efe27e7c1c9a2bf5c872b29c90f76849b51jim <note type="warning">You will need to take care of the DNS
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen resolution - Apache does
860b4efe27e7c1c9a2bf5c872b29c90f76849b51jim not handle name resolution. You'll need either to create CNAME
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen records for each hostname, or a DNS wildcard record. Creating DNS
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen records is beyond the scope of this document.</note>
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen<p>The internal <code>tolower</code> RewriteMap directive is used to
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowenensure that the hostnames being used are all lowercase, so that there is
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowenno ambiguity in the directory structure which must be created.</p>
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen<p>Parentheses used in a <directive
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowenmodule="mod_rewrite">RewriteCond</directive> are captured into the
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowenbackreferences <code>%1</code>, <code>%2</code>, etc, while parentheses
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowenused in <directive module="mod_rewrite">RewriteRule</directive> are
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowencaptured into the backreferences <code>$1</code>, <code>$2</code>,
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowenetc.</p>
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen<p>
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowenAs with many techniques discussed in this document, mod_rewrite really
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowenisn't the best way to accomplish this task. You should, instead,
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowenconsider using <module>mod_vhost_alias</module> instead, as it will much
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowenmore gracefully handle anything beyond serving static files, such as any
860b4efe27e7c1c9a2bf5c872b29c90f76849b51jimdynamic content, and Alias resolution.
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen</p>
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen </dd>
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen </dl>
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen</section>
83ac24b7851b96b187504b65c4c22372d1fd1b6arbowen
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen<section id="simple.rewrite"><title>Dynamic
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen Virtual Hosts Using <module>mod_rewrite</module></title>
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen <p>This extract from <code>httpd.conf</code> does the same
b50e762594a8fd10a9462148254c2e8a372422f3rbowen thing as <a href="#per-hostname">the first example</a>. The first
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen half is very similar to the corresponding part above, except for
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen some changes, required for backward compatibility and to make the
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen <code>mod_rewrite</code> part work properly; the second half
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen configures <code>mod_rewrite</code> to do the actual work.</p>
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen <p>Because <code>mod_rewrite</code> runs before other URI translation
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen modules (e.g., <code>mod_alias</code>), <code>mod_rewrite</code> must
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen be told to explicitly ignore any URLs that would have been handled
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen by those modules. And, because these rules would otherwise bypass
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen any <code>ScriptAlias</code> directives, we must have
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen <code>mod_rewrite</code> explicitly enact those mappings.</p>
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh<highlight language="config">
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh# get the server name from the Host: header
0a0fd048f54df3e9053942466292da579524c1fdhumbedoohUseCanonicalName Off
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh# splittable logs
0a0fd048f54df3e9053942466292da579524c1fdhumbedoohLogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
aaed4ebc116f5862aff2610a809a9a9e9a28f50ecoarCustomLog "logs/access_log" vcommon
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar&lt;Directory "/www/hosts"&gt;
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh # ExecCGI is needed here because we can't force
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh # CGI execution in the way that ScriptAlias does
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh Options FollowSymLinks ExecCGI
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh&lt;/Directory&gt;
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh
0a0fd048f54df3e9053942466292da579524c1fdhumbedoohRewriteEngine On
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh# a ServerName derived from a Host: header may be any case at all
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteMap lowercase "int:tolower"
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh## deal with normal documents first:
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh# allow Alias /icons/ to work - repeat for other aliases
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteCond "%{REQUEST_URI}" "!^/icons/"
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh# allow CGIs to work
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteCond "%{REQUEST_URI}" "!^/cgi-bin/"
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh# do the magic
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteRule "^/(.*)$" "/www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1"
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh## and now deal with CGIs - we have to force a handler
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteCond "%{REQUEST_URI}" "^/cgi-bin/"
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteRule "^/(.*)$" "/www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1" [H=cgi-script]
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh</highlight>
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen</section>
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen<section id="xtra-conf"><title>Using a Separate Virtual Host Configuration File</title>
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen <p>This arrangement uses more advanced <module>mod_rewrite</module>
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen features to work out the translation from virtual host to document
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen root, from a separate configuration file. This provides more
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen flexibility, but requires more complicated configuration.</p>
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen <p>The <code>vhost.map</code> file should look something like
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen this:</p>
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen<example>
ffe8ed2cd88be2c415ddef11f50394c52df9190ctrawickcustomer-1.example.com /www/customers/1<br />
ffe8ed2cd88be2c415ddef11f50394c52df9190ctrawickcustomer-2.example.com /www/customers/2<br />
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen# ...<br />
ffe8ed2cd88be2c415ddef11f50394c52df9190ctrawickcustomer-N.example.com /www/customers/N<br />
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen</example>
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen <p>The <code>httpd.conf</code> should contain the following:</p>
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh<highlight language="config">
0a0fd048f54df3e9053942466292da579524c1fdhumbedoohRewriteEngine on
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteMap lowercase "int:tolower"
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh# define the map file
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteMap vhost "txt:/www/conf/vhost.map"
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh# deal with aliases as above
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteCond "%{REQUEST_URI}" "!^/icons/"
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteCond "%{REQUEST_URI}" "!^/cgi-bin/"
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteCond "${lowercase:%{SERVER_NAME}}" "^(.+)$"
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh# this does the file-based remap
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteCond "${vhost:%1}" "^(/.*)$"
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteRule "^/(.*)$" "%1/docs/$1"
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteCond "%{REQUEST_URI}" "^/cgi-bin/"
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteCond "${lowercase:%{SERVER_NAME}}" "^(.+)$"
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteCond "${vhost:%1}" "^(/.*)$"
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteRule "^/(.*)$" "%1/cgi-bin/$1" [H=cgi-script]
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh</highlight>
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen</section>
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen
860b4efe27e7c1c9a2bf5c872b29c90f76849b51jim</manualpage>