In different server environments, the PHP $_SERVER['DOCUMENT_ROOT']
super global sometimes has a trailing slash and sometimes it does not. I would have thought this issue is directly related to how the Apache DocumentRoot
is defined in the httpd.conf
file:
i.e. I would have thought that if httpd.conf
contains no trailing slash:
<VirtualHost *:8880>
DocumentRoot /var/www/live/current
...
then echo $_SERVER['DOCUMENT_ROOT']
should give /var/www/live/current
and if httpd.conf
does contain a trailing slash:
<VirtualHost *:8880>
DocumentRoot /var/www/live/current/
...
then echo $_SERVER['DOCUMENT_ROOT']
should give /var/www/live/current/
This is the case on Ubuntu 10.04 but on RHEL 5.5 a trailing slash is added to $_SERVER['DOCUMENT_ROOT']
even if none was defined on Apache.
Any idea why this happens? Is there a configuration parameter that I'm missing?
For reference:
- PHP 5.3.3 of RHEL (issue occurs): PHP 5.3.3 (cli) (built: Jul 23 2010 16:26:53)
- PHP version of Ubuntu (no issue): PHP 5.3.2-1ubuntu4.2 with Suhosin-Patch (cli) (built: May 13 2010 20:03:45)
I have no idea why the slash is changing between your virtual hosts. By the way, is it important ? Just add a new slash to your programs (remove if a double slash is present) and the problem is solved.
I use
Document root in an Apache environment can be defined in more than one place.
Yes,
httpd.conf
contains these settings, but they can be overwritten as this file is used for the default configuration.I'd suggest you go check the vhost configuration under
vhosts.d
andsites-available
directories.http://httpd.apache.org/docs/2.0/mod/core.html says: The DocumentRoot should be specified without a trailing slash.
The proposed solution:
does not work in all of the installations.
For example, in my case:
Same problem as before.
May be you should modify the first instruction in:
thistle
Dom's answer is a solution to this issue, however stefanvesca's statement is the reason why in the different environments you are experiencing the double '//'. On your local machine, within your .conf file where you set up your virtual host, you most likely added the / at the end of the defined document root, while the person who set up your other environment did not, or vice versa.
Eitherway, when using php's $_SERVER['DOCUMENT_ROOT'] you get the apache environment value which is the result of the configuration. That is the reason for the '/' in one environment and '//' in another.
I would say it's assumed DOCUMENT_ROOT has no trailing slash.
This value is passed from the web server configuration
apache
DocumentRoot /var/www/html
That implies we should have a leading slash to path we add to it.
Knowing that a double slash '//' anywhere in the path has no consequence (when related to filesystem... in a http url, there could be cases where is has some glitches)
$ cat /etc//issue Debian GNU/Linux 9 \n \l
When there is a trailing slash to DOCUMENT_ROOT we can blame sysadmin on something that has no consequence :)
And safely ignore it?