加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – codeigniter将非www重定向到www

发布时间:2020-12-13 17:39:27 所属栏目:PHP教程 来源:网络整理
导读:我看到 .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=30
我看到 .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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读