codeigniter将非www重定向到www

bra*_*age 4 php .htaccess codeigniter

我看到.htaccess将非WWW重定向到WWW保留URI字符串,但它对我不起作用

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

RewriteCond %{HTTP_HOST} !^www\.mysite\.com$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
Run Code Online (Sandbox Code Playgroud)

还尝试过:

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

编辑:

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

if (substr($_SERVER['SERVER_NAME'], 0, 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;

}
Run Code Online (Sandbox Code Playgroud)

Alf*_*ava 6

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

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

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

if(substr($_SERVER['SERVER_NAME'],0,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
}
Run Code Online (Sandbox Code Playgroud)