Take the 2-minute tour ×

What I want to do is have dynamically created virtualhosts based on a username and subfolder, for example if I were to create the following folder:


The following domain will be mapped to the previous folder as its webroot


Aside from creating a script that checks for new folders, creating the matching VirtualHost in the Apache configuration, and restarting HTTPD, is there a config-friendly method of accomplishing this?

-- EDIT -- Thank you @kashani for suggesting mod_vhost_alias. It worked perfectly.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

You might want to look into mod_vhost_alias. http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html

The config in your case should look like this, though I haven't tested it.

VirtualDocumentRoot /home/%2/www/%1 

and for logging

CustomLog logs/access_log dynamic_vhosts

That'll put the vhost the request came into at the front of each log entry. You can adjust the rest of the setting to match what you need. If %V doesn't work, try %v. I don't believe there is a way to split out into separate log files at least with the standard Apache mod_log.

share|improve this answer
Thanks @kashani! After working with mod_vhost_alias for a while, that seemed to do the trick! My only concern now is if the project_folder directory has capital letters in it the apache usually translates EXAMPLE.username.domain.com as "example.username.domain.com" when in fact the directory may be "/home/username/www/EXAMPLE" –  Workman Feb 3 '11 at 21:25
I can't think of a way to solve that one, but glad to hear mod_vhost_alias worked out for you. –  kashani Feb 3 '11 at 21:38
One more semi-related question. Is there any way to also have virtual mapping of error and access logs using mod_vhost_alias? –  Workman Feb 9 '11 at 20:24
put the logging in the answer for better formating. –  kashani Feb 10 '11 at 18:47
@Workman, mod_speling might help with the case sensitivity of the folders. But I've never used it in conjunction with mod_vhost_alias: httpd.apache.org/docs/2.2/mod/mod_speling.html –  Mat Feb 10 '11 at 19:01

With "ServerAlias *.example.com" and the Virtual User Hosts RewriteRule example, I believe you can easily setup apache to do what you wish


share|improve this answer
Can you do this with local hostnames as well? Such as PROJECT_FOLDER.USER.dev (where 'dev' is the name of the machine)? –  user69389 Feb 3 '11 at 18:37

Your Answer


