Я адаптировал следующее из CS50:
function redirect($destination){
// handle URL
if (preg_match("/^https?:\/\//", $destination)){
header("Location: " . $destination);
}else if (preg_match("/^\//", $destination)){
// handle absolute path
$protocol = (isset($_SERVER["HTTPS"])) ? "https" : "http";
$host = $_SERVER["HTTP_HOST"];
header("Location: $protocol://$host$destination");
}else if (preg_match("/^www/",$destination)){
//handle www addresses (added by rss)
$protocol = (isset($_SERVER["HTTPS"])) ? "https" : "http";
header("Location: $protocol://$destination");
}else{
// handle relative path
// adapted from http://www.php.net/header
$protocol = (isset($_SERVER["HTTPS"])) ? "https" : "http";
$host = $_SERVER["HTTP_HOST"];
$path = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
header("Location: $protocol://$host$path/$destination");
}
// exit immediately since we're redirecting anyway
/****************
print($_SERVER["HTTPS"].'<br>');
print($_SERVER["HTTP_HOST"].'<br>');
print($host.'<br>');
print($path.'<br>');
print($destination.'<br>');
print_r2($_SERVER);
*****************/
exit;
}