Codeigniter Core Mod for $_SERVER[‘DOCUMENT_ROOT’] on GoDaddy subdomains with $_SERVER[‘SUBDOMAIN_DOCUMENT_ROOT’]

I was tinkering around with a CodeIgniter project earlier today and at some point, I needed to use PHP’s file_exists() function. I tend to use $_SERVER[‘DOCUMENT_ROOT’] for my starting location for finding files located in the web-viewable areas (makes sense huh?). This makes it easy to get the file url by simply replacing the document_root from the path of the filename which results in a nice absolute path for linking. In this particular instance, I was checking for an image to show in place of a heading tag. Granted the code was simple and straight forward, but it could not get PHP to locate the file and I knew it was there.

After a few minutes of checking/rechecking my code. I decided to echo $_SERVER[‘DOCUMENT_ROOT’]. To my suprise, GoDaddy’s configuration does not update $_SERVER[‘DOCUMENT_ROOT’] to reflect the paths for a subdomain. They instead add another variable that I was never aware of. In comes $_SERVER[‘SUBDOMAIN_DOCUMENT_ROOT’]. Granted I had never seen this variable so I did not even know it existed until I did some quick Google searches.

After realizing that this practice was not exactly uncommon, I had to figure out how I should update my CodeIgniter distribution to work using only $_SERVER[‘DOCUMENT_ROOT’] and not having to worry about $_SERVER[‘SUBDOMAIN_DOCUMENT_ROOT’]. Here is the code I added to the top of “/index.php”…

if (!empty($_SERVER['SUBDOMAIN_DOCUMENT_ROOT'])) {
    $_SERVER['DOCUMENT_ROOT'] = $_SERVER['SUBDOMAIN_DOCUMENT_ROOT'];
}

It’s simple and easy to implement this method in any of your scripts. I do suggest it is set at the top of each script (via include() function).

You won’t have to worry about the subdomain root variable when moving your site from a subdomain to the main domain. I primarily use them as development environments while updating live sites to ensure maximum compatibility. So when I move the site fro subdomain to the main domain, no changes will be necessary 🙂

3 thoughts on “Codeigniter Core Mod for $_SERVER[‘DOCUMENT_ROOT’] on GoDaddy subdomains with $_SERVER[‘SUBDOMAIN_DOCUMENT_ROOT’]”

  1. THANK YOU!!! I was pulling out my hair on a WordPress plugin that was failing to find files even though I knew the path (URL) was correct. The DOCUMENT_ROOT looked good too but still was not working. Wouldn’t you know, it was a GoDaddy account!?!

    Anyway, knowing about this little documented SUBDOMAIN_DOCUMENT_ROOT server variable was the trick.

    Thanks!

    Cheers,

    Jeff

  2. I have project in Codeigniter and i have problem with Godaddy subdomain as above. Here are the details;

    url: [removed]
    username: [removed]
    password: [removed]

    I would appreciate if you find out the issue

    1. I am not free support. If you need me to look around, I have to charge.

      On a quick glance… check your htaccess file. Looks more like URL rerouting issues than a subdomain problem.

      PS: Don’t include usernames/passwords in comments.

Leave a Reply

Your email address will not be published. Required fields are marked *