Apache/PHP:通过PHP检索ServerName设置的任何方法 [英] Apache/PHP: Any way to retrieve the ServerName setting via PHP
问题描述
我有一个如下所示的Apache虚拟主机:
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>
我有以下文件:
/srv/web/host1.example.com/pub/test.php
我正在请求以下URL(请注意,我使用的是ServerAlias
,而不是ServerName
:
I am requesting the following URL (notice that I am using the ServerAlias
, not the ServerName
:
http://host2.example.com/test.php
我想让test.php
以某种方式输出ServerName
变量(host1.example.com
).有什么办法吗?
I'd like to have test.php
output the ServerName
variable somehow (host1.example.com
). Is there any way to do that?
推荐答案
显然,当您通过别名访问VirtualHost时,没有任何原始痕迹
任何$_SERVER
变量中的SERVER_NAME
.
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
这应将$_SERVER["MY_HOST"]
的值设置为正确的主机名.不过,不能保证,我从未在实践中尝试过此方法.
this should set the value of $_SERVER["MY_HOST"]
to the correct host name. No guarantees though, I have never tried this in practice.
这篇关于Apache/PHP:通过PHP检索ServerName设置的任何方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!