selfnote: How to handle multiple domains with one apache vhost in a smart way

Today a customer asked for hosting several domains with different content on one apache virtual host. The little tricky part was, that the sites should not be accessed by subdirs like http://domain20.org/subdir20/ and http://domain21.org/subdir21/ which can be done with a little script which parses the $_SERVER['HTTP_HOST']. So mod_rewrite is an option.
To make it user accessable, we use .htaccess. This requires “AllowOverride +FileInfo” to be set for the DocumentRoot in the vhost config. The .htaccess in DocumentRoot could look something like:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} domain10.org$ [OR]
RewriteCond %{HTTP_HOST} domain11.org$
RewriteCond %{REQUEST_FILENAME} !dir1/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) dir1/$1 [L]
RewriteCond %{HTTP_HOST} domain20.org$ [OR]
RewriteCond %{HTTP_HOST} domain21.org$
RewriteCond %{REQUEST_FILENAME} !dir2/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) dir2/$1 [L]

Creative Commons License
The selfnote: How to handle multiple domains with one apache vhost in a smart way by Cyconet Blog, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Terms and conditions beyond the scope of this license may be available at blog.waja.info.

2 Responses to “selfnote: How to handle multiple domains with one apache vhost in a smart way”


  1. 1 Gustavo Noronha (kov)

    Why not create a new VHost, anyway? It would be a lot less work, and would be much saner.

  2. 2 cyco

    O sorry, I missed your comment.

    Short answer. Imagine you have customers, which are paying per vhost. This is a costeffective solution.

Leave a Reply




Too Cool for Internet Explorer