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

$realpath = realpath ($_SERVER['DOCUMENT_ROOT']."/");
$realpath = str_replace ("//", "/", $realpath);
  • 3
    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
  • 1
    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 Nocke Oct 14 '15 at 10:56
  • rtrim($_SERVER['DOCUMENT_ROOT'],'/\\').'/' should be slightly faster than all those things realpath can do. – Frank Nocke Oct 14 '15 at 11:02

http://httpd.apache.org/docs/2.0/mod/core.html says: The DocumentRoot should be specified without a trailing slash.


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.


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'])."/";


  • 1
    rtrim($_SERVER['DOCUMENT_ROOT'],'/\\').'/' should be slightly faster than all those things realpath can do. – Frank Nocke Oct 14 '15 at 11:01

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


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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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