URL与PHP $ _SERVER ['DOCUMENT_ROOT']不同

时间:2019-06-21 07:02:54

标签: php vagrant virtualbox centos7

我正在使用Oracle VM VirtualBox + Vagrant + CentOS 7.0 + PHP。

当我进入页面http://192.168.33.10/public/index.php时:

header('Location: '.$_SERVER['DOCUMENT_ROOT'] .'/public/index.php');

这应该重定向到public/public/index.php,但是我不知道为什么它重定向到/vagrant/public/public/index.php

我该如何解决?

1 个答案:

答案 0 :(得分:1)

这是错误的! 首先,您以错误的方式使用header()

正确:

header('Location: URL');

您使用了错误的变量:

$_SERVER['DOCUMENT_ROOT']

返回机器上的绝对本地路径。

$ _ SERVER ['SERVER_NAME']更合适,但请记住在其前面添加http / https

但是:

您无需添加当前服务器名-只需添加路径

header('Location: /public/index.php');

这将始终重定向到HTTP(S)://YOURSERVER/public/index.php

您还可以使用相对路径,只需删除斜杠