PHP 301重定向位置URI格式


PHP 301 redirect location URI format

这是header('Location: '),特别是./的正确URI吗?

header ('HTTP/1.1 301 Moved Permanently');
header ('Location: ./');

谢谢。

您也可以使用:

header('Location: /', false, 301);

我假设你想重定向到"主页",那应该是/而不是。/

您必须根据规范使用绝对URI,因此下面的内容应该适合您:

// check if the server is secure or not to determine URL prefix
if(isset($_SERVER['HTTPS']) and 'on' === $_SERVER['HTTPS']) {
    $location = 'https://';
} else {
    $location = 'http://';
}
// get the servers base URL
$location .= $_SERVER['SERVER_NAME'] . '/';
// grab the current URI without a file name in it
$location .= dirname($_SERVER['REQUEST_URI']) . '/';
header('Location: ' . $location);
exit();