Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a bunch of subdomains set up on my company's intranet. I set up each subdomain manually. I would like certain subdomains to get created automatically.

For example, if I have /home/jason/foo and /home/jason/bar, I want to be able to go to foo.jason.mydomain.local and bar.jason.mydomain.local without having to manually set up those subdomains. (The "jason" part can be hard-coded; I just want the "foo"/"bar" part to be automatic.)

I've found this page, which seems to be pretty much what I want, but I can't bridge the gap between their examples and what I'm doing. Can someone please help me out?

Thanks, Jason

share|improve this question

In the example... they're making use of the FQDN... and setting up a default site that pulls from a sub-folder. Basically... anything thrown at that IP will look for a folder with a matching name as the DNS name. i.e. would resolve to /somewhere/

I'm not 100% sure... but I believe any site that does not have a /somewhere/_/ folder will throw up an error... rather than a default web site.

There's actually several different ways to implement it. (as described in that page you linked)

Using the mod_rewrite... will effectively rewrite the URL to a different path than was anticipated. i.e. would rewrite the addresses to something like (you can make it work both ways so the end-user would never see the URL... but for permissions & such apache sees it that way.) with that model... you would have a folder (or alias) in your document root for that site.

Using the mod_vhost_alias works similarly... but it makes assumptions that may not always work. i.e. I don't think you can have a "default" root.... where everything that doesn't match gets dumped to. but and would get auto-mapped to /somedirectory/ and /somedirectory/ respectively.

The key thing to make note of is the variables used in the configuration. $0 translates to the full-domain name of the site requested. i.e. VirtualDocumentRoot /www/%0/ would automatically dump you to the /www/ directory (if browsing to and mod-rewrite would pattern match on it & also rewrite the path to whatever directory.

share|improve this answer
up vote 0 down vote accepted

I've answered my own question.

Here's what I put in my Apache config:

<VirtualHost *:80>
  VirtualDocumentRoot /var/www/hosts/%0
  ServerName jason.mydomain.local
  ServerAlias *.jason.mydomain.local

Then, in /var/www/hosts, I put a directory called jason.mydomain.local. That's all it took.

share|improve this answer
In fact, the above works even without the ServerName directive. – Jason Swett Sep 20 '10 at 17:39
My understanding is that you should use UseCanonicalName. Does your example work for any subdomain like "foo.jason.mydomain.local", "bar.jason.mydomain.local", "baz.jason.mydomain.local"? – Stefan Lasiewski Sep 20 '10 at 21:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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