区别与没有www, 301重定向与codeigniter和htaccess


Difference with and without www, 301 redirect with codeigniter and htaccess

我有一个域,显示不同的页面,如果我离开www。没有www页面卡住了,所以我想把http://example.com重定向到http://www.example.com

我的问题是,我如何改变htaccess,使它重定向到www版本?

这是我的.htaccess:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index'.php|robots'.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]

RewriteEngine On:

之后添加以下行
RewriteCond %{HTTP_HOST} !^www'.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

但是请记住,重定向规则只是对您的应用程序的一种妥协,而不是解决您的页面冻结问题。

一个朋友解决了我的问题,他在现有的代码下面添加了以下代码:

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www'. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

现在一切都很完美!完整的.htaccess文件现在看起来像:

DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index'.php|robots'.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www'. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]