Php codeigniter将非www重定向到www

Php codeigniter将非www重定向到www,php,.htaccess,codeigniter,Php,.htaccess,Codeigniter,我看到了,但对我不起作用 如果我转到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] Re

我看到了,但对我不起作用

如果我转到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]

编辑:

以下是我使用的代码,基于阿方索·鲁巴卡娃的回答:

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;

}

请尝试在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
}
编辑 我看错了你的问题,答案是:

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
}

在codeigniter的.htaccess文件中包含这两行代码,以将非www url重定向到www

RewriteCond %{HTTP_HOST} !^www\.(.*)
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
i、 e完整的.htaccess文件如下->

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{HTTP_HOST} !^www\.(.*)
 RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

重新启动发动机
重写cond%{HTTP_HOST}^www\(*)
重写规则(.*)http://www.%{HTTP_HOST}/$1[R=301,L]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$index.php/$1[L]
使用此代码

#Redirect to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我知道,这个问题已经有了公认的解决办法。但是,如果域也有一些子域,则会出现问题。如果您的codeigniter也处理子域,子域也将重定向到www

这里是这个案例的答案。假设您的域名是www.thedomain.com,代码应该是:

$sna = explode(".", $_SERVER['SERVER_NAME']);
if($sna[0]=="thedomain"){
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://www.thedomain.com/".$_SERVER['REQUEST_URI']);
}

您可以在所有codeigniter网站中使用以下代码

`


重新启动发动机
重写cond%{HTTP_HOST}^www\(*)
重写规则(.*)http://www.%{HTTP_HOST}/$1[R=301,L]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$index.php/$1[L]

`

我不能仅使用htaccess执行此操作吗?是的,您可以。这只是用更熟悉、问题更少的东西做这件事的一种方法。。。这不需要每次从服务器迁移时都进行调整。谢谢,它可以工作。顺便说一句,我删除了“else”语句,并在if:]的末尾使用了一个“exit;”哦。唯一的问题是,如果我转到“mysite.com/site”,我会被重定向到“mysite.com//site”,并得到一个404错误。请从
yourdomain.tdl/”服务器中删除
/
[
我很高兴它很有用。这个
//site/
看起来像是一个格式错误的链接。您可能希望在代码中更正(在视图中?)为了避免搜索引擎将其作为索引。哦..链接的格式没有错误,必须是带有CI routes或htaccess的内容才能删除索引。php..在浏览器上键入地址时的行为是相同的
http://www.site.com//site/
?我以为你的意思是我使用
标题(“位置:http://www.“$\u SERVER['SERVER\u NAME']。$\u SERVER['REQUEST\u URI'];
使其动态化
<IfModule mod_rewrite.c>
RewriteEngine On
 RewriteCond %{HTTP_HOST} !^www\.(.*)
 RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>