10705fbc225dcf590c9ba24a4f04de221358060arbowen<?xml-stylesheet type="text/xsl" href="/style/manual.en.xsl"?>
6896b3498e144d33f374efe93e6bab2669af535fnd<!-- $LastChangedRevision$ -->
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 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.
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen<title>Dynamic mass virtual hosts with mod_rewrite</title>
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<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
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>-->
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>
7aae1084ea6db741a017571545f5be0914a03148lgentis <title>Virtual Hosts For Arbitrary Hostnames</title>
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 <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
0a0fd048f54df3e9053942466292da579524c1fdhumbedoohRewriteEngine on
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteMap lowercase "int:tolower"
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteCond "${lowercase:%{<strong>HTTP_HOST</strong>}}" "^www\.<strong>([^.]+)</strong>\.example\.com$"
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>
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>
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>,
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.
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen Virtual Hosts Using <module>mod_rewrite</module></title>
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 <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>
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh# get the server name from the Host: header
0a0fd048f54df3e9053942466292da579524c1fdhumbedoohUseCanonicalName Off
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh# splittable logs
0a0fd048f54df3e9053942466292da579524c1fdhumbedoohLogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh # ExecCGI is needed here because we can't force
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh # CGI execution in the way that ScriptAlias does
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh Options FollowSymLinks ExecCGI
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh</Directory>
0a0fd048f54df3e9053942466292da579524c1fdhumbedoohRewriteEngine On
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh# a ServerName derived from a Host: header may be any case at all
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteMap lowercase "int:tolower"
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## 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]
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen<section id="xtra-conf"><title>Using a Separate Virtual Host Configuration File</title>
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 <p>The <code>vhost.map</code> file should look something like
03a229dcd0273c27b17f6c75bd0ad5425a8c7964rbowen <p>The <code>httpd.conf</code> should contain the following:</p>
0a0fd048f54df3e9053942466292da579524c1fdhumbedoohRewriteEngine on
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteMap lowercase "int:tolower"
0a0fd048f54df3e9053942466292da579524c1fdhumbedooh# define the map file
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}" "^(/.*)$"
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteCond "%{REQUEST_URI}" "^/cgi-bin/"
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteCond "${lowercase:%{SERVER_NAME}}" "^(.+)$"
5d01f40ffd657dd2ac567aacd93cabd162ddfa79coarRewriteCond "${vhost:%1}" "^(/.*)$"
860b4efe27e7c1c9a2bf5c872b29c90f76849b51jim</manualpage>