简体   繁体   English

PHP 301重定向位置URI格式

[英]PHP 301 redirect location URI format

Is this a correct URI for the header('Location: ') , specifically ./ ? 这是header('Location: ')的正确URI header('Location: ') ,特别是./

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

Thank you. 谢谢。

You can also use: 您还可以使用:

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

I assume you want to redirect to the 'homepage', that'd be / instead of ./ 我假设你想重定向到'主页',那就是/而不是./

You must use an absolute URI according to the spec so something like the following should work for you: 您必须根据规范使用绝对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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM