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

前端 未结 1 428
小鲜肉
小鲜肉 2020-12-21 06:42
if (condition)
{
#lol. Some code here
}
else
{       
header(\"Location:/\");//i\'m trying to redirect to the root
}

Redirect works perfectly on lo

相关标签:
1条回答
  • 2020-12-21 07:43

    From the manual:

    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'] and dirname() 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;
    ?>
    
    0 讨论(0)
提交回复
热议问题