2

I have two virtual hosts declared: localhost and test.

Both use the SetEnv directive and point to the same DocumentRoot:

NameVirtualHost *:80

<VirtualHost _default_:80>
    DocumentRoot "/Users/mf/Sites/test"
    SetEnv FROM_ROOT 'i like tacos'             ## <- SetEnv
</VirtualHost>

<VirtualHost *:80>
    ServerName localhost    
    DocumentRoot "/Users/mf/Sites/test"
    <Directory "/Users/mf/Sites/test">
        Options Includes FollowSymLinks  
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    SetEnv FROM_VHOST 'set in localhost vhost'  ## <- SetEnv
</VirtualHost>

<VirtualHost *:80>
    ServerName test
    DocumentRoot "/Users/mf/Sites/test"
    <Directory "/Users/mf/Sites/test">
        Options Includes FollowSymLinks  
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    SetEnv FROM_VHOST 'set in test vhost'        ## <- SetEnv
</VirtualHost>

I have a test file /Users/mf/Sites/test/dump.php:

var_dump( $_SERVER );

For http://test/ everything works as I'd expect, $_SERVER['FROM_VHOST'] is available. dump.php outputs:

array (size=33)
  'UNIQUE_ID' => string 'UY7bHMCoAXIAABkbAkMAAAAC' (length=24)
  'FROM_VHOST' => string 'set in test vhost' (length=17)
  'HTTP_HOST' => string 'test' (length=4)
…

For http://localhost/ things break down:

array (size=34)
  'UNIQUE_ID' => string 'UY7bA8CoAXIAABkZAhQAAAAA' (length=24)
  'FROM_ROOT' => string 'i like tacos' (length=5)
  'HTTP_HOST' => string 'localhost' (length=9)
…

?

  1. Why does localhost not have a FROM_VHOST environment variable?
  2. Why does only localhost see the FROM_ROOT environment variable?
2

Few things to try:

1) This could be because you have localhost setup as a hostname which would more and likely trigger the default, check in /etc/hostname or optionally use a FQDN such as web.localhost

2) Another thing to try is putting Default at the bottom rather than top as those higher get more priority.

3) Obvious fix don't use localhost as a vhost :)

I'm not too confident with my answer but give a go (I'll revisit this when I get more time}

  • Re (1), ServerName localhost:80 is set outside of any block in the httpd.conf? I agree, (3) is my preferred choice, as I'm using MAMP and it generates the httpd.conf from a template. I'm mostly asking to learn, and to help understand what's going on in case I want to take the training wheels off and fly without a GUI ;-) – Mark Fox May 12 '13 at 2:51

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.