2

I am seeing a strange behavior when accessing $_SERVER['DOCUMENT_ROOT'] on a subdomain. It contains a duplicate folder name which does not exist.

Here is the the relevant output of $_SERVER:

[DOCUMENT_ROOT] => /var/www/sub.domainname.com/sub.domainname.com
[SCRIPT_FILENAME] => /var/www/sub.domainname.com/index.php

It should be:

[DOCUMENT_ROOT] => /var/www/sub.domainname.com

Here is the virtual host configuration:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName sub.domainname.com

    DocumentRoot /var/www/sub.domainname.com
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /var/www/sub.domainname.com>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>    
</VirtualHost>
Derek
  • 3,438
  • 1
  • 17
  • 35
  • 1
    That's why you should define a constant like `ROOT` and rely on that one, rather than `DOCUMENT_ROOT` – Yang Jun 09 '14 at 15:48
  • 4
    Anyone care to explain their down-votes? – Derek Jun 09 '14 at 15:49
  • 1
    I don't get why this question gets downvoted. They probably do that just for fun. +1 for the question – Yang Jun 09 '14 at 15:50
  • @Scorpion where should this constant be defined? – Derek Jun 09 '14 at 15:50
  • 1
    You can define that in `index.php` like `define('ROOT', dirname(__FILE__))` – Yang Jun 09 '14 at 15:51
  • 1
    I don't see any reason why this variable is considered *not reliable*. Of course there are workarounds but I'm interested in the reason for the wrong path. Can you paste your virtual host configuration for the subdomain? (+1) – Daniel W. Jun 09 '14 at 15:53
  • @DanFromGermany How don't you see? If you can see it right here – Yang Jun 09 '14 at 15:54
  • 1
    @Scorpion but why is that? I've never come to the situation where DOCUMENT_ROOT was wrong. Neither with nginx, nor with Apache or any other server I had under administration. – Daniel W. Jun 09 '14 at 15:54
  • 1
    @DanFromGermany i was thinking the same thing. It's fairly common to rely on variables such as this one for auto-discovery. In fact it's pretty necessary to allow us to build easily portable base code. `DOCUMENT_ROOT` is perfectly reliable as long as you have set the webserver up correctly (and if you haven't, patching in a ROOT constant is just lazy, fix the issue!) – Lee Jun 09 '14 at 15:56
  • @DanFromGermany I have added the vhost config to my question. – Derek Jun 09 '14 at 15:59
  • 1
    check for .htaccess files also - a wrong document root is (as indicated by the answer below) a webserver config problem. – AD7six Jun 09 '14 at 16:01
  • @Lee sometimes webserver control is not ours (ie. working on a client's server), so the reliability can not always be assumed. – Derek Jun 09 '14 at 16:04

1 Answers1

2

This issue is likely caused by a mis-configured webserver, the PHP documentation says this $_SERVER['DOCUMENT_ROOT'] contains the script directory as defined in the servers config file. Which may therefore be different to the actual script directory location.

DOCUMENT_ROOT
The document root directory under which the current script is executing, as defined in the server's configuration file.

Source: PHP Reserved Variables $_SERVER[] Manual Page

AeroX
  • 3,387
  • 2
  • 25
  • 39