开发者

PHP 301 redirect location URI format

开发者 https://www.devze.com 2023-02-28 08:22 出处:网络
Is this a correct URI for the header(\'Locat开发者_运维知识库ion: \'), specifically ./? header (\'HTTP/1.1 301 Moved Permanently\');

Is this a correct URI for the header('Locat开发者_运维知识库ion: '), specifically ./?

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:

// 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();
0

精彩评论

暂无评论...
验证码 换一张
取 消