使用 PHP 将 WWW 重定向到非 WWW

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2079457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:55:32  来源:igfitidea点击:

WWW to non-WWW Redirect with PHP

phpredirectno-www

提问by Alix Axel

I want to redirect all www.domain.com requests to domain.com with PHP, basically:

我想用 PHP 将所有 www.domain.com 请求重定向到 domain.com,基本上:

if (substr($_SERVER['SERVER_NAME'], 0, 4) === 'www.')
{
    header('Location: http://' . substr($_SERVER['SERVER_NAME'], 4)); exit();
}

However I do want to maintain the requested URL like in SO, for e.g.:

但是我确实想像 SO 一样维护请求的 URL,例如:

http://www.stackoverflow.com/questions/tagged/php?foo=bar

Should redirect to:

应该重定向到:

http://stackoverflow.com/questions/tagged/php?foo=bar

I don't want to rely on .htaccesssolutions, and I'm unsure which $_SERVERvars I'd have to use to make this happen. Also, preserving the HTTPS protocol would be a plus.

我不想依赖.htaccess解决方案,我不确定$_SERVER我必须使用哪些变量来实现这一点。此外,保留 HTTPS 协议将是一个加分项。

How should I do this?

我该怎么做?

采纳答案by Tyler Carter

$pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if ($_SERVER["SERVER_PORT"] != "80")
{
    $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} 
else 
{
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
header('Location: '. $pageURL);

Would redirect the user to the exact same page, www. intact.

将用户重定向到完全相同的页面,www。完整。

So, to get rid of the www. , we just replace one line:

所以,摆脱www。,我们只替换一行:

$pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if ($_SERVER["SERVER_PORT"] != "80")
{
    $pageURL .= substr($_SERVER['SERVER_NAME'], 4).":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} 
else 
{
    $pageURL .= substr($_SERVER['SERVER_NAME'], 4).$_SERVER["REQUEST_URI"];
}
return $pageURL;

And that should work.

这应该有效。

By the way, this is the method that is recommended by Google, as it keeps https://intact, along with ports and such if you do use them.

顺便说一句,这是谷歌推荐的方法,因为它保持https://完整,如果你确实使用了端口等。



As Gumbo pointed out, he uses $_SERVER['HTTP_HOST']as it comes from the headers instead of the server, thus $_SERVER['SERVER_*']is not as reliable. You could replace some$_SERVER['SERVER_NAME']with $_SERVER['HTTP_HOST'], and it should work the same way.

正如 Gumbo 指出的那样,他使用的$_SERVER['HTTP_HOST']是来自标头而不是服务器的信息,因此$_SERVER['SERVER_*']不太可靠。您可以取代一些$_SERVER['SERVER_NAME']$_SERVER['HTTP_HOST'],它应该工作方式相同。

回答by Gumbo

Try this:

尝试这个:

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;
}

回答by amar nath jena

if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) &&                                                                                         (strtolower($_SERVER['HTTPS']) != 'off')) {
           $https = 1;
} else {
            $https = 0;
}
if (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.') {
            redirect(($https?'https://':'http://') .'www.' .                                                                   $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
}

if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) {
           $https = 1;
} else {
            $https = 0;
}
if (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.') {
            redirect(($https?'https://':'http://') .'www. ' . $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
}