标头(“位置:/”);重定向适用于本地主机,但不适用于远程服务器


header("Location:/"); redirect works on localhost, but not on remote server

if (condition)
{
#lol. Some code here
}
else
{       
header("Location:/");//i'm trying to redirect to the root
}

重定向在本地主机上完美运行,但在远程服务器上则不然。也许使用$_SERVER更好?即使我选择与带有重定向的文件位于同一目录中的文件,此重定向也不起作用。希望你帮助我:)

从手册:

HTTP/1.1 需要一个绝对 URI 作为参数 » 位置:包括方案、主机名和绝对路径,但某些客户端接受相对 URI。您通常可以使用$_SERVER['HTTP_HOST']$_SERVER['PHP_SELF']dirname()自己从相对URI创建绝对URI:

<?php
/* Redirect to a different page in the current directory that was requested */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/''');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>