Dynamic mass virtual hosts with mod_rewrite
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 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 RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/docs/$1
and now deal with CGIs - we have to force a handler RewriteCond %{REQUEST_URI} ^/cgi-bin/ RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1 [H=cgi-script]
define the map file RewriteMap vhost txt:/www/conf/vhost.map
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} ^(/.*)$ RewriteRule ^/(.*)$ %1/docs/$1 RewriteCond %{REQUEST_URI} ^/cgi-bin/ RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$ R
This document supplements the mod_rewritereference documentation. It describes how you can use
to create dynamically configured virtual hosts.mod_rewrite
In this recipe, we assume that we'll be using the hostname www.
for each user, and serve their content out of SITE.example.com/home/
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.
are captured into the backreferences RewriteCond%1
, etc, while parentheses used in
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
instead, as it will much more gracefully handle anything beyond serving static files, such as any dynamic content, and Alias resolution. modvhostalias
runs before other URI translation modules (e.g., mod_alias
must be told to explicitly ignore any URLs that would have been handled by those modules. And, because these rules would otherwise bypass any ScriptAlias
explicitly enact those mappings.
file should look something like this:
customer-1.example.com /www/customers/1
should contain the following:
RewriteEngine on RewriteMap lowercase int:tolower # define the map file RewriteMap vhost txt:/www/conf/vhost.map # deal with aliases as above RewriteCond %{REQUESTURI} !^/icons/ RewriteCond %{REQUESTURI} !^/cgi-bin/ RewriteCond ${lowercase:%{SERVERNAME}} ^(.+)$ # this does the file-based remap RewriteCond ${vhost:%1} ^(/.)$ RewriteRule ^/(.)$ %1/docs/$1 RewriteCond %{REQUESTURI} ^/cgi-bin/ RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$ RewriteCond ${vhost:%1} ^(/.)$ RewriteRule ^/(.)$ %1/cgi-bin/$1 [H=cgi-script]
Modules | Directives | FAQ | Glossary | Sitemap
Apache HTTP Server Version 2.4
Dynamic mass virtual hosts with mod_rewrite
Available Languages: en | fr
Virtual Hosts For Arbitrary Hostnames
We want to automatically create a virtual host for every hostname which resolves in our domain, without having to create new VirtualHost sections.
Dynamic Virtual Hosts Using mod_rewrite
This extract from apache2.conf does the same thing as the first example. The first half is very similar to the corresponding part above, except for some changes, required for backward compatibility and to make the mod_rewrite part work properly; the second half configures mod_rewrite to do the actua
Using a Separate Virtual Host Configuration File
This arrangement uses more advanced mod_rewrite 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.