Question

I have an Apache virtual host that looks like this:

<VirtualHost *:80>
    ServerName host1.example.com
    ServerAlias host2.example.com
    DocumentRoot /srv/web/host1.example.com/pub
</VirtualHost>

I have the following file:

/srv/web/host1.example.com/pub/test.php

I am requesting the following URL (notice that I am using the ServerAlias, not the ServerName:

http://host2.example.com/test.php

I'd like to have test.php output the ServerName variable somehow (host1.example.com). Is there any way to do that?

Was it helpful?

Solution

Apparently, when you access a VirtualHost through the alias, there is no trace of the original SERVER_NAME in any of the $_SERVER variables.

The only idea that comes to mind is setting an environment variable:

SetEnv MY_HOST host1.example.com

this should set the value of $_SERVER["MY_HOST"] to the correct host name. No guarantees though, I have never tried this in practice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top