php – codeigniter将非www重定向到www

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – codeigniter将非www重定向到www脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我看到 .htaccess Redirect non-WWW to WWW preserving URI string,但它对我不起作用

如果我去mysITe.COM/site/something我会被重定向到mysite.com/something

RewriteCond %{HTTP_HOST} !^www\.mysite\.com$
rewriterule (.*) http://www.mysite.com/$1 [R=301,L]

还尝试过:

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www.mysite.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]

编辑:

这是我使用的代码,基于Alfonso Rubalcava的回答:

if (substr($_SERVER['SERVER_NamE'],3) != 'www')
{

    if ($_SERVER['REQUEST_URI'] == '//site/')
    {
        header('HTTP/1.1 301 Moved PErmanently');
        header('Location: http://www.site.com/site/');
        exit;
    }

    header('HTTP/1.1 301 Moved Permanently');
    header('Location: http://www.site.com' . $_SERVER['REQUEST_URI']);
    exit;

}

解决方法

在index.PHP中尝试,在开头:

if(substr($_SERVER['SERVER_NAME'],3)=="www"){
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://yourdomain.tdl/".$_SERVER['REQUEST_URI']);
}else{
    //the current contents of your file
}

编辑
我读错了你的问题,答案是:

if(substr($_SERVER['SERVER_NAME'],3)!="www"){
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://www.yourdomain.tdl/".$_SERVER['REQUEST_URI']);
}else{
    //the current contents of your file
}

脚本宝典总结

以上是脚本宝典为你收集整理的php – codeigniter将非www重定向到www全部内容,希望文章能够帮你解决php – codeigniter将非www重定向到www所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。