2

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

Jason Swett
  • 1,458
  • 4
  • 23
  • 37

2 Answers2

1

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
</VirtualHost>

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

Jason Swett
  • 1,458
  • 4
  • 23
  • 37
  • 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](http://httpd.apache.org/docs/2.2/vhosts/mass.html#simple). 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
0

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. test.local.com would resolve to /somewhere/test.local.com/

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. test.somewhere.com would rewrite the addresses to something like www.something.com/test.something.com/ (you can make it work both ways so the end-user would never see the domain.com/sub-domain.com URL... but for permissions & such apache sees it that way.) with that model... you would have a sub-domain.com 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 test.domain.com and test2.domain.com would get auto-mapped to /somedirectory/test.domain.com and /somedirectory/test2.domain.com 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/some.domain.com/ directory (if browsing to some.domain.com) and mod-rewrite would pattern match on it & also rewrite the path to whatever directory.

TheCompWiz
  • 7,349
  • 16
  • 23