CodeIgniter - 如何使用会话重定向到非 WWW URL

CodeIgniter - How to redirect into non WWW URL with sessions

在我的 CodeIgniter 项目中,当我尝试从一个控制器重定向到另一个 controller/function 时,一个额外的 "WWW" 出现在 URL 的前面。使用该 WWW-URL,我无法访问在非 WWW 站点中创建的旧会话信息。

我的重定向代码如下。

public function test()
{
    $this->load->helper('url');
    redirect('/Cat/view');
}

URL 如下所示

http://app.mysite.com/login                   << where I set session info
http://www.app.mysite.com/index.php/Cat/View  << After redirect

如何在 CodeIgnitor 中安全地从非 www 站点重定向到非 www 站点?

我通过修改我的 .htaccess 文件以重写 URL 而不使用 WWW 来解决问题.

下面代码的第一部分是删除"WWW",第二部分是删除"index.php" 来自 URL

DirectoryIndex index.php

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

RewriteCond  !^(index\.php|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]