Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)
share|improve this question
up vote 5 down vote accepted

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

$realpath = realpath ($_SERVER['DOCUMENT_ROOT']."/");
$realpath = str_replace ("//", "/", $realpath);
share|improve this answer
Thanks - this is want we have done in the end. – Tom Aug 24 '10 at 17:34
You could simply use $realpath = realpath($_SERVER['DOCUMENT_ROOT']);. It automatically removes all multiple slashes and also the trailing one. If you always want a trailing slash concatenate it to the result of the above call to realpath(). Not inside it. – flu Sep 24 '14 at 8:26
I can confirm the same issue (php 5.5) between a Hosteurope host (Ubuntu I think): has slash... and Windows 7: no slash (fairly recent XAMPP install) – Frank N Oct 14 '15 at 10:56
rtrim($_SERVER['DOCUMENT_ROOT'],'/\\').'/' should be slightly faster than all those things realpath can do. – Frank N Oct 14 '15 at 11:02

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 and sites-available directories.

share|improve this answer says: The DocumentRoot should be specified without a trailing slash.

share|improve this answer

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.

share|improve this answer

The proposed solution:

$realpath = realpath ($_SERVER['DOCUMENT_ROOT']."/");
$realpath = str_replace ("//", "/", $realpath);

does not work in all of the installations.

For example, in my case:

$_SERVER['DOCUMENT_ROOT']  = "/www/site/"
$_SERVER['DOCUMENT_ROOT']."/" = "/www/site//"
realpath("/www/site//") = "/www/site"
str_replace("//", "/", "/www/site") = "/www/site"

Same problem as before.

May be you should modify the first instruction in:

$realpath = realpath ($_SERVER['DOCUMENT_ROOT'])."/";


share|improve this answer
rtrim($_SERVER['DOCUMENT_ROOT'],'/\\').'/' should be slightly faster than all those things realpath can do. – Frank N Oct 14 '15 at 11:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.