Apache/PHP: Anyway to retrieve the ServerName setting via PHP

心已入冬 提交于 2020-01-06 08:19:28

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/4834141/apache-php-anyway-to-retrieve-the-servername-setting-via-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!