PHP 301重定向位置URI格式

Fra*_*isc 8 php location uri header

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

header ('HTTP/1.1 301 Moved Permanently');
header ('Location: ./');
Run Code Online (Sandbox Code Playgroud)

谢谢.

Wes*_*orp 7

您还可以使用:

header('Location: /', false, 301);
Run Code Online (Sandbox Code Playgroud)

我假设你想重定向到'主页',那就是/而不是./

  • 根据标准,需要绝对URL.这将有效,但无效 (3认同)
  • 正如@Pekka所提到的那样,请参阅http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30 (2认同)

Tre*_*non 6

您必须根据规范使用绝对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();
Run Code Online (Sandbox Code Playgroud)