我想用PHP将所有的www.domain.com请求重定向到domain.com,基本上是:
if (substr($_SERVER['SERVER_NAME'], 0, 4) === 'www.')
{
header('Location: http://' . substr($_SERVER['SERVER_NAME'], 4)); exit();
}
但是,我确实希望像这样维护请求的URL,例如:
http://www.stackoverflow.com/questions/tagged/php?foo=bar
应重定向至:
http://stackoverflow.com/questions/tagged/php?foo=bar
我不想依赖.htaccess
解决方案,我也不确定必须使用哪些$_SERVER
变量才能实现这一点。此外,保留HTTPS协议将是一个加分。
我该怎么做呢?
发布于 2010-01-17 08:08:45
试试这个:
if (substr($_SERVER['HTTP_HOST'], 0, 4) === 'www.') {
header('Location: http'.(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ? 's':'').'://' . substr($_SERVER['HTTP_HOST'], 4).$_SERVER['REQUEST_URI']);
exit;
}
发布于 2015-08-19 13:27:25
如果(isset($_SERVER‘’HTTP_X_FORWARDED_PROTO‘)&strtolower($_SERVER’‘HTTP_X_FORWARDED_PROTO’) == 'https') || (isset($_SERVER"HTTPS") && strtolower($_SERVER“HTTPS”)==“on”){
*$https= 1;
}否则{
* $https = 0;
}
if (substr($_SERVER‘’HTTP_HOST‘,0,4) !==’www..‘){
在这里我们可以看到它的标题(‘location:’,($https?'https://':'http://') .‘www..’。*$_SERVER‘’REQUEST_URI‘);
}
https://stackoverflow.com/questions/2079457
复制相似问题