[英]PHP How to get the root url of a subdomain
I used to have my website into a domain like : www.mydomain.com
我曾经将我的网站设置为一个域名,例如:
www.mydomain.com
But now i've created a subdomain 'test' : www.test.mydomain.com
and i moved the whole project files there. 但是现在我创建了一个子域“ test”:
www.test.mydomain.com
,并将整个项目文件移到了那里。
So far so good, the problem is that on my php libraries where i call db connection etc, i use the $_SERVER['DOCUMENT_ROOT']
which gives me the URL without the subdomain , ex 到目前为止,很好的问题是,在我调用数据库连接等的php库中,我使用了
$_SERVER['DOCUMENT_ROOT']
,该URL提供了没有子域的URL ,例如
$file = $_SERVER['DOCUMENT_ROOT'];//gives me www.mydomain.com
But i need to get the root with the subdomain, like : www.test.mydomain.com 但是我需要使用子域来获得根目录,例如 :www.test.mydomain.com
I dont want to make a trick like to split the url and add the subdomain thing. 我不想像把URL拆分并添加子域一样。
Any help would be appreciated. 任何帮助,将不胜感激。
You can try both: 您可以同时尝试:
$_SERVER["SERVER_NAME"]
$_SERVER['HTTP_HOST']
Both will return the domain with the subdomain. 两者都将返回带有子域的域。
$_SERVER["SERVER_NAME"]
The name of the server host under which the current script is executing.
正在执行当前脚本的服务器主机的名称。 If the script is running on a virtual host, this will be the value defined for that virtual host.
如果脚本在虚拟主机上运行,则将是为该虚拟主机定义的值。
$_SERVER['HTTP_HOST']
Contents of the Host: header from the current request, if there is one.
主机的内容:当前请求中的标头(如果有)。
You can read up on the difference between $_SERVER["SERVER_NAME"]
and $_SERVER['HTTP_HOST']
at HTTP_HOST vs. SERVER_NAME . 您可以在HTTP_HOST与SERVER_NAME上阅读
$_SERVER["SERVER_NAME"]
和$_SERVER['HTTP_HOST']
之间的区别。
$_SERVER['DOCUMENT_ROOT']
will not work as stated in documentation $_SERVER['DOCUMENT_ROOT']
将无法按照文档中所述操作
The document root directory under which the current script is executing, as defined in the server's configuration file.
服务器配置文件中定义的当前脚本正在其下执行的文档根目录。
More information on $_SERVER
at http://php.net/manual/en/reserved.variables.server.php . 有关
$_SERVER
更多信息,请$_SERVER
http://php.net/manual/zh/reserved.variables.server.php 。
$_SERVER['DOCUMENT_ROOT']
does not contain the server name! $_SERVER['DOCUMENT_ROOT']
不包含服务器名称! It contains - as the name says - the DOCUMENT_ROOT
. 顾名思义,它包含
DOCUMENT_ROOT
。 This is the directory where the filesystem starts that your webserver serves. 这是Web服务器服务的文件系统启动的目录。
You should try $_SERVER['SERVER_NAME']
instead. 您应该尝试使用
$_SERVER['SERVER_NAME']
。 This should work if your virtual server setup is correct. 如果您的虚拟服务器设置正确,这应该可以工作。
Finally, after two hours searching , I spoke to the system admin and he cleared that he actually created an alias domain which it actually reacts as : www.test.mydomain.com
. 最终,经过两个小时的搜索,我与系统管理员交谈,他清除了自己实际上创建的别名域的情况 ,该域的实际反应为:
www.test.mydomain.com
。
I guess that is why i get the www.mydomain.com
without the subdomain when i call $_SERVER['DOCUMENT_ROOT']
. 我猜这就是为什么当我调用
$_SERVER['DOCUMENT_ROOT']
时得到不www.mydomain.com
域的www.mydomain.com
原因。
Thanks, case closed. 谢谢,结案了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.