Dynamic virtual hosts

Problems:

You want to have each user on your system automatically have username.domain.com mapped to their home directory.

Recipe:

RewriteEngine On
# Skip www.domain.com
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule ^/(.*)$  /home/%1/www/$1 [L]

Discussion:

The first RewriteCond skips requests for content from www.domain.com, which we want to be handled via the regular mechanism. It may also be useful to exclude other hostnames which you don't want to get mapped to home directories.

The second RewriteCond exists to capture which hostname was requsted. When matches are captured (by using parentheses) in a RewriteCond, the captured pattern goes into the variables %1, %2, %3, and so on. In this case, a request for a hostname of username.domain.com will result in %1 being set to username.

Finally, our RewriteRule captures the entire requested URL, and causes it to be served out of that user's home directory. For example, a request for http://rbowen.domain.com/foo/bar.html will result in the file /home/rbowen/www/foo/bar.html being served.

It is important to note that you will still need to make the necessary changes to your DNS server to make these hostnames work. Generally, this would be done by creating a wildcard DNS entry mapping *.domain.com to this server. You will need to consult the documentation for your particular DNS server as to how to accomplish this.

For a more complex mapping, see RewriteMap

Canonical Hostnames

Problem:

You have a virtual host with several values for ServerAlias. However, you'd like to force the use of a particular hostname, in preference to other hostnames which may be used to reach the same site. For example, if you wish to force the use of www.example.com instead of example.com, you might use a variant of the following recipe.

Recipe:

# For sites running on a port other than 80
RewriteCond %{HTTP_HOST}   !^www\.example\.com [NC]
# make sure the host header is not empty (old http/1.0 requests)
# in order to prevent infinite redirection loops.
RewriteCond %{HTTP_HOST}   !=""
RewriteCond %{SERVER_PORT} !=80
RewriteRule ^/(.*)         http://www.example.com:%{SERVER_PORT}/$1 [L,R=301]

# And for a site running on port 80
RewriteCond %{HTTP_HOST}   !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST}   !=""
RewriteRule ^/(.*)         http://www.example.com/$1 [L,R=301]

# or combined
RewriteCond %{HTTP_HOST}    !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST}    !=""
RewriteCond :%{SERVER_PORT} ^(:80|(:[0-9]+))$
RewriteRule ^/(.*)          http://www.example.com%2/$1 [L,R=301]

Discussion:

There are a variety of reasons that you may wish to force the use of one particular host name in preference to another. The most commons ones are

  • search engine optimation (example.com and www.example.com are two different sites)
  • cookies which will work only on a particular hostname
  • SSL, which necessitates that the hostname be used which matches the SSL certificate
  • Authentication, which needs to be on the right hostname for the browser to recognize it
  • No labels