#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    95
    Rep Power
    30

    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:

    PHP Code:
    include file.php 
    instead of this:

    PHP Code:
    include $_SERVER['document_root'] . '/www/versions/' $version '/file.php'
    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?

    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:

    PHP Code:
    define('CLIENT_DOCUMENT_ROOT'$_SERVER['document_root'] . '/www/versions/' $version); 
    This all has to be done automatically. I can't edit the http.conf and restart Apache every time a new subdomain is added.

    Any thoughts?
  2. #2
  3. mod_dev_shed
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    14,817
    Rep Power
    1100
    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):
    PHP Code:
    $versions = array(
      
    'a.domain.com' => 2,
      
    'b.domain.com' => 3
    );

    $version $versions[$_SERVER['HTTP_HOST']]; 
    # Jeremy

    Explain your problem instead of asking how to do what you decided was the solution.

IMN logo majestic logo threadwatch logo seochat tools logo