标头(“位置:/");重定向适用于本地主机,但不适用于远程服务器 [英] 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
可能更好吗?即使我选择与重定向文件位于同一目录中的文件,此重定向也不起作用.希望你能帮助我:)
Redirect works perfectly on localhost, but not on remote server.
May be it's all better to use $_SERVER
?
This redirect wouldn't work even if i choose file in the same directory as file with redirect.
Hope you help me :)
推荐答案
来自手册:
HTTP/1.1 需要一个绝对 URI 作为 » Location 的参数:包括方案、主机名和绝对路径,但一些客户端接受相对 URI.您通常可以使用 $_SERVER['HTTP_HOST']
、$_SERVER['PHP_SELF']
和 dirname()
从自己的亲戚:
HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use
$_SERVER['HTTP_HOST']
,$_SERVER['PHP_SELF']
anddirname()
to make an absolute URI from a relative one yourself:
<?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;
?>
这篇关于标头(“位置:/");重定向适用于本地主机,但不适用于远程服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!