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
- If a folder exists, set that folder as the document root.
If it doesn't exist, set
~/server/wwwas the document root.
Multi-level subdomains need to route to the base level subdomain. For example,
http://another.sub.domain.tld/should default to
If no subdomains are requested, use
/home/ [domain w/o extension] /server/ [subdomain, default 'www']
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
http://subdomain.domain.tld- looks for
http://www.subdomain.domain.tld- looks for
But it fails to satisfy
http://domain.tld/. (looks for
~/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.