I run a web server using Ubuntu/Apache, and I'm looking for a way to dynamically host subdomains across multiple domains. I've been messing around with Apache for a few days, and came short of a complete fix.
Here's the workflow I'm looking for:
If HTTP_HOST has a prepended 'www', ignore it.
If it includes a subdomain, check if a corresponding folder exists in
~/server/
- If a folder exists, set that folder as the document root.
If it doesn't exist, set
~/server/www
as the document root.Multi-level subdomains need to route to the base level subdomain. For example,
http://another.sub.domain.tld/
should default to~/server/sub
If no subdomains are requested, use
~/server/www
Folder structure:
/home/ [domain w/o extension] /server/ [subdomain, default 'www']
Example: http://sub.domain.tld/
matches /home/domain/server/sub
Here's what I have so far, using mod_vhost_alias:
UseCanonicalName Off
VirtualDocumentRoot /home/%-2/server/%-3
That alone satisfies:
http://www.domain.tld/
- looks for~/server/www
http://subdomain.domain.tld
- looks for~/server/subdomain
http://www.subdomain.domain.tld
- looks for~/server/subdomain
But it fails to satisfy http://domain.tld/
. (looks for ~/server/domain
)
Also, if ~/server/subdomain
doesn't exist, it returns a 404.
In short, I would like each subdomain to have its own folder in ~/server/
or default to ~/server/www
if non-existant.
I want to avoid using mod_rewrite because it doesn't change the document root; it just prepends a directory to the request. However, I may just change the folder structure as a last resort.