【问题标题】:htaccess rewrite not working as it should with wwwhtaccess 重写不能正常工作,因为它应该与 www
【发布时间】: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


【解决方案1】:

如评论中所述。

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

【讨论】:

    【解决方案2】:

    这里并没有真正提供比 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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-13
      • 2013-09-10
      • 2017-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多