homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Dynamic Subdomains and PHP's DOCUMENT ROOT
Subdomains made from directories, with correctly reported PHP document root

 8:46 pm on May 2, 2008 (gmt 0)

We are starting to set up an internal development environment on our office. We are using the TLD dev locally. We've got DNS routing STDs to individual computers in the office.

So I've got *.matt.dev pointed at my local machine.
My web root is at

I would like to have subdomains pointed at directories in my web dir.

test.matt.dev would point to /Users/mdeclair/Sites/test/

I've seen many ways to do this, and have "successfully" done it in the two ways shown below.


RewriteEngine on
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^(.+)\.matt\.dev(.*) /Users/mdeclair/Sites/$1$2


NameVirtualHost *
<VirtualHost *>
ServerName matt.dev
ServerAlias *.matt.dev
VirtualDocumentRoot /Users/mdeclair/Sites/%-3
UseCanonicalName Off

The problem I've run into is that when testing my PHP scripts, the variable $_SERVER['DOCUMENT_ROOT'] comes up as /Users/mdeclaire/Sites/ without the appropriate subdomain included.

So that is my hurdle.

Bonus question!
I'd love to have the subdomaining system recursive, such that
hey.dude.matt.dev =>
and furthermore
man.bear.pig.matt.dev =>
and so on.



 3:31 pm on May 8, 2008 (gmt 0)

Is the file actually stored in /Users/mdeclair/Sites/test ? or is it rewritten / served from somewhere else? I am thinking you might need to switch to PATH_TRANSLATED rather than DOCUMENT_ROOT.


 3:47 pm on May 8, 2008 (gmt 0)

I would rather have my PHP environment behave a closely to my clients environment as possible. Cuts down on the bugs.


 3:55 pm on May 8, 2008 (gmt 0)

Which is correct will depend on where the script is actually running though, correct?

If the script is running in /Users/mdeclair/Sites/test/ then DOCUMENT_ROOT is correct, but if the script is running in /Users/mdeclaire/Sites/ directly, then you will need to use PATH_TRANSLATED (or SCRIPT_FILENAME depending on Apache version and AcceptPathInfo setting)... It was the reason I was asking where the php file was actually located anyway.


 4:08 pm on May 8, 2008 (gmt 0)

Maybe I misunderstood.

I've got the dir: /Users/mdeclair/Sites/, and within it, many directories, one per project. So:


I'd like to use http://coolsite.matt.dev, and have it resolve to /Users/mdeclair/Sites/coolsite.com/index.php, with $_SERVER['DOCUMENT_ROOT'] = /Users/mdeclair/Sites/coolsite.com


 4:17 pm on May 8, 2008 (gmt 0)

I understand (I think), but for DOCUMENT_ROOT to = /Users/mdeclair/Sites/coolsite.com the current running script will have to physically be located in /Users/mdeclair/Sites/coolsite.com.

Let me give you a 'for instance'...

If you do not have a physical directory called /Users/mdeclair/Sites/coolsite.com, with the file located within the directory, DOCUMENT_ROOT will return the incorrect location.

IOW If you 'dynamically create' the directories for use, and then rewrite the directory to another location to access the script you will receive the location of the script with DOCUMENT_ROOT, but will receive the 'virtual location' using PATH_TRANSLATED or SCRIPT_FILENAME.


If the script is located at /Users/mdeclair/Sites/awesomesites.php but runs when /User/mdeclair/Sites/awesomesite.com/ is opened, you will receive the incorrect location using DOCUMENT_ROOT, because it gives you the physical server path to the actual location of the current script, not a rewritten or dynamically generated path.

If the script is physically located at /User/mdeclair/Sites/awesomesite.com/index.php then DOCUMENT_ROOT *should* give you the correct path/location. If the script is physically stored in another location on the server, then you will have to use one of the other server variables to return the current 'dynamic' location of the request.


 4:25 pm on May 8, 2008 (gmt 0)

The 'short' version is where are you actually storing the file you are testing on your server?

If it is in /User/mdeclair/Sites/ then everything is working correctly. If it is physically stored in /User/mdeclair/Sites/awesomesite.com/ right now, then I will have to think for a minute.


 10:49 pm on May 8, 2008 (gmt 0)

I think my situation is your latter explanation.

file location: /Users/mdeclair/Sites/awesomesite/test.php
<?php echo $_SERVER['DOCUMENT_ROOT']; ?>
current output:
desired output:


 3:00 pm on May 19, 2008 (gmt 0)

The problem here is that when using mod_vhost_alias to create dynamic domains, the internal Apache DocumentRoot/DOCUMENT_ROOT value for that VirtualHost/Domain gets set to the default httpd.conf value [whatever it may be]. There is no way to change or fix this behavior. Only script workarounds.


 3:13 pm on May 19, 2008 (gmt 0)

Thanks for you help. I've since just reverted to using VirtualHosts for my subdomains.


 3:25 pm on May 19, 2008 (gmt 0)

Maybe "SCRIPT_FILENAME" will work for you.

$parts = explode( '/', $_SERVER['SCRIPT_FILENAME'] );
array_pop( $parts );
$path = implode( '/', $parts );


 10:44 pm on May 19, 2008 (gmt 0)

Thank you, but I do not want to change the script.

I would want to have the environment mimic the end production environment, so that deployment would not involve any changes to the script.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved