I want to offer custom subdomains for my customers. I can add virtual host configuration for each subdomain but it gets a overwhelming job as the number of users increase.

What I currently use is like this:

NameVirtualHost *:80
<VirtualHost *:80>
  DocumentRoot /var/www/mysite/user1
  ServerName user1.mysite.com
<VirtualHost *:80>
  DocumentRoot /var/www/mysite/user2
  ServerName user2.mysite.com
<VirtualHost *:80>
  DocumentRoot /var/www/mysite
  ServerName example.com
  ServerAlias www.mysite.com

Is there a better way of configuring subdomains?

Is it possible to make a simple check and direct an non-existing virtual host to the main site?

PS: Some other Virtualhosts still need to be defined this way.

1 Answer 1



Within your VirtualHost definition:

VirtualDocumentRoot /var/www/multipledomains_root/%0

Then anything that comes at the HTTP_HOST header apache will try to match in %0

Look at (http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html) to have a complete list of what you can do.

  • does it work along with regular VirtualHost configuration
    – mustafa
    Mar 27, 2013 at 18:05
  • yes, and you should if you have multiple rules for subdomain hosting.
    – Marcel
    Mar 27, 2013 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.