Forum Moderators: phranque
So I've got *.matt.dev pointed at my local machine.
My web root is at
/Users/mdeclair/Sites/
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.
First:
RewriteEngine on
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^(.+)\.matt\.dev(.*) /Users/mdeclair/Sites/$1$2
Second:
NameVirtualHost *
<VirtualHost *>
ServerName matt.dev
ServerAlias *.matt.dev
VirtualDocumentRoot /Users/mdeclair/Sites/%-3
UseCanonicalName Off
</VirtualHost>
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 =>
/Users/mdeclair/Sites/dude/hey/,
/Users/mdeclair/Sites/pig/bear/man/,
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.
I've got the dir:
/Users/mdeclair/Sites/, and within it, many directories, one per project. So:
/User/mdeclair/Sites/awesomesite.com/
/User/mdeclair/Sites/coolsite.com/
/User/mdeclair/Sites/nosoaweomesite.com/
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
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.
EG
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.