htaccess重写不能正常工作与www

时间:2012-07-05 20:09:54

标签: apache .htaccess codeigniter

我正在开发基于CodeIgniter的应用程序。我的htacess看起来像这样:

RewriteEngine On
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteBase /
RewriteRule ^(.*)$ /index.php?$1 [L]
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

这在大多数情况下都很有用,我可以访问这样的控制器和方法:

http://example.com/controller/method/args

但是,如果它附加了www,就像这样

http://www.example.com/foo

重定向到

http://example.com/index.php?foo

显示的页面始终是我网站的索引页面。

我添加了最后一对RewriteCond / RewriteRule只是为了删除www(如果存在)。

我不太确定如何让它以我想要的方式工作,所以

http://www.example.com/foo

只是重定向到

http://example.com/foo

修改

从我的CI配置文件:

$config['base_url'] = 'http://example.com/';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';

2 个答案:

答案 0 :(得分:3)

如评论中所述。

您的重写规则RewriteRule ^(.*)$ /index.php?$1 [L]应放在底部。由于您的[L]标志,订单在这种情况下很重要。

答案 1 :(得分:1)

在这里提供的不仅仅是Steven Lu ......但是为了更有意义而清理了你的订单

RewriteEngine On
RewriteBase /

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

RewriteCond  %{REQUEST_FILENAME} !-f
# You don't really need the !-d (directory) flag here
RewriteRule ^(.*)$ /index.php [L] # Could put QSA in here too, but CI doesn't play too nice with query strings 
相关问题