【发布时间】:2012-07-05 20:09:53
【问题描述】:
我正在开发基于 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';
【问题讨论】:
-
你在
application/config/config.php中设置了$config['index_page']吗? -
是的。它设置为
$config['index_page'] = ''; -
投票结束的人能否解释一下原因?
-
给定的条件应该有效。经htaccess.madewithlove.be 测试,结果为i.imgur.com/1NCfv.png
-
您的重写规则
RewriteRule ^(.*)$ /index.php?$1 [L]将被放置在底部。在这种情况下,由于您的 [L] 标志,订单很重要。
标签: apache .htaccess codeigniter