Automatic subdomains with Apache – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.
But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about apache-2.2, wildcard-subdomain, , , .
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?
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.
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.