我正在使用这个,但是每当我尝试使用 www 打开时,它都会给我一个错误:
if($_SERVER['HTTP_HOST'] == "sub.mydomain.com")
header("Location: mydomain.com/sub/");
我哪里出错了?
我希望用户在键入 sub.mydomain.com 或 www.sub.mydomain.com 时重定向
您需要执行正确的标头调用。
喜欢这个:
if(strpos('sub.example.com', $_SERVER['HTTP_HOST']) !== false){
header("location: http://example.com/sub/");
}
编辑:根据这篇文章中的信息:您也可以改用此函数:
function get_subdomain($url=""){
if($url==""){
$url = $_SERVER['HTTP_HOST'];
}
$parsedUrl = parse_url($url);
$host = explode('.', $parsedUrl['path']);
$subdomains = array_slice($host, 0, count($host) - 2 );
return implode(".", $subdomains);
}
if('sub' != get_subdomain()){
header("location: http://example.com/sub/");
}