January 30th, 2012, 05:46 AM
Different document root per subdomain
I used to be on shared hosting with cPanel. I had multiple subdomains that all pointed to different locations:
a.domain.com -> /www/versions/2
b.domain.com -> /www/versions/3
c.domain.com -> /www/versions/3
d.domain.com -> /www/versions/1
I did this by using cPanel to create the subdomain every time, then set the document root for each of them. That worked fine...using different document roots let me easily include files without worrying about which version to include them from.
I could do this:
instead of this:
Now I've moved over to a VPS and want to do this right. Individually setting up subdomains is slow, so I intend to use a wildcard so that all subdomains point to my server. I don't think I can set different document roots this way, though. I have an entry in the database for each sudomain with a column for version...what's the best way to set this up?
include $_SERVER['document_root'] . '/www/versions/' . $version . '/file.php';
I'm thinking that I hit the database to check the version, then do something like this, but I'm not confident it's the right way:
This all has to be done automatically. I can't edit the http.conf and restart Apache every time a new subdomain is added.
define('CLIENT_DOCUMENT_ROOT', $_SERVER['document_root'] . '/www/versions/' . $version);
March 11th, 2012, 12:33 PM
If you don't want to restart Apache every time you need a new version, then I'd recommend staying away from having Apache do this for you. If you did want to use Apache, you could use mod_rewrite to set an environment variable based on the domain, and you should be able to do this in .htaccess.
Although a simple PHP array that matches host to version would probably be easiest (and prevent a database query):
$versions = array(
'a.domain.com' => 2,
'b.domain.com' => 3
$version = $versions[$_SERVER['HTTP_HOST']];
Explain your problem instead of asking how to do what you decided was the solution.