mattalxndr
mattalxndr

Reputation: 9408

Apache/PHP: Any way to retrieve the ServerName setting via PHP

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?

Upvotes: 1

Views: 4565

Answers (2)

Lorenz Meyer
Lorenz Meyer

Reputation: 19895

With our webhost, the default admin email is webmaster@<virtual-hostname>. Thanks to this, I can get the virtuel host name through

$virtualhostname = substr($_SERVER['SERVER_ADMIN'], strpos($_SERVER['SERVER_ADMIN'], '@') + 1);

Upvotes: 0

Pekka
Pekka

Reputation: 449385

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.

Upvotes: 3

Related Questions