DOCUMENT_ROOT trailing slash

307 views
Skip to first unread message

Aaron Cooper

unread,
Mar 16, 2009, 11:11:32 PM3/16/09
to nzp...@googlegroups.com
Hi all,
 
We have a new dedicated server and $_SERVER['DOCUMENT_ROOT'] is returning the correct document root with no trailing slash.
 
On all our old servers, it would return it with a trailing slash. So we have alot of sites that require a bit of adjustment for this new box
 
Is this configurable and would it be managed by php.ini or Apache?
 
Arguments for and against the trailing slash (or another more portable way of obtaining the docroot) is welcome.
 
Cheers
Aaron

Paul Bennett

unread,
Mar 17, 2009, 1:35:01 AM3/17/09
to nzp...@googlegroups.com
from memory, you can set / alter the document root in httpd.conf

edit 
restart apache
problem solved...

Carlos

unread,
Mar 17, 2009, 4:36:26 AM3/17/09
to nzp...@googlegroups.com
Aaron Cooper a écrit :

If you want this behaviour at the server level, you can use mod_dir with
the DirectorySlash directive. The details are in the Apache server
documentation.

Regards,
Carlos

Simon Holywell

unread,
Mar 17, 2009, 5:10:04 AM3/17/09
to nzp...@googlegroups.com

Jared Armstrong

unread,
Mar 17, 2009, 7:38:33 AM3/17/09
to NZ PHP Users Group

dirname(__FILE__) i use inmost cases, or similar depending on file
location, and works for most situatons. Obviously, this is a quite
different concept than document root.

Keith Allpress

unread,
Mar 22, 2009, 5:07:23 AM3/22/09
to nzp...@googlegroups.com
Technology idiosyncracies not worth trying to patch over when you can
take control ?
Write it using a PHP include file for any page that needs it ?
Then it will return correctly!
Without the slash why not. :)
Keith

Nathan Kennedy

unread,
Mar 22, 2009, 3:50:31 PM3/22/09
to nzp...@googlegroups.com

If you must use $_SERVER[‘DOCUMENT_ROOT’] and if you have no control over the configuration of Apache or PHP, you could use the following hack to ensure that it always has the trailing slash:

 

$_SERVER['DOCUMENT_ROOT'] .= (substr($_SERVER['DOCUMENT_ROOT'],-1)=='/')?'':'/';

 

Thanks,

Nathan.

http://control.kennedytechnology.com

Aaron Cooper

unread,
Mar 22, 2009, 5:51:46 PM3/22/09
to nzp...@googlegroups.com
Thanks Nathan,
 
I eventually found this hack. But I was just hoping to change this on the server level so we don't have to mess with the code base of 60+ existing sites at all.
 
Keith, we are talking about migrating existing sites here, not coding new ones.
 
Someone over on the cPanel forums has responded and I'm looking at an option mentioned there. It seems that it should be something set on the server globally.
 
Thanks for responses everyone.
 
Cheers
Aaron

php&delphi

unread,
Mar 23, 2009, 3:28:25 AM3/23/09
to NZ PHP Users Group
Yes, you get good idea to solve it, in Apache, you can set
DocumentRoot without /

nice point!!!

Jin

lazydada

unread,
Apr 27, 2009, 4:57:40 PM4/27/09
to NZ PHP Users Group
THANK YOU! (And your fancy-pants PHP).
Peek-a-boo DOCUMENT_ROOT trailing slashes? No problem.

> $_SERVER['DOCUMENT_ROOT'] .=
> (substr($_SERVER['DOCUMENT_ROOT'],-1)=='/')?'':'/';

A hack made in heaven.

Aaron Cooper

unread,
Apr 27, 2009, 5:27:23 PM4/27/09
to nzp...@googlegroups.com
> Peek-a-boo DOCUMENT_ROOT trailing slashes? No problem.

If hacks are your thing and you don't mind hacking every single site you put
on the server. (I explained I had 60+ sites to migrate)

This fixes the symptom, not the problem.



----- Original Message -----
From: "lazydada" <gde...@gmail.com>
To: "NZ PHP Users Group" <nzp...@googlegroups.com>
Sent: Tuesday, April 28, 2009 8:57 AM
Subject: [phpug] Re: DOCUMENT_ROOT trailing slash


>
Reply all
Reply to author
Forward
0 new messages