【问题标题】:.htaccess mod_rewrite not working correctly.htaccess mod_rewrite 无法正常工作
【发布时间】:2012-08-05 15:00:00
【问题描述】:

我在我的开发机器上使用 Wamp 来开发一个项目。这是我的 .htaccess:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L] 
RewriteBase /project/

这是放在 localhost/project 中的,因此任何像 localhost/project/something 这样的请求都会被路由到 localhost/project/index.php/something

但是,它们被路由到 localhost/index.php/something

我怀疑这与我使用 RewriteBase 的方式有关。我该如何解决?

【问题讨论】:

    标签: apache .htaccess codeigniter mod-rewrite wamp


    【解决方案1】:

    您需要删除规则中的前导斜杠:

    # no slash---------v
    RewriteRule ^(.*)$ index.php?$1 [L] 
    

    Apache 会猜测 RewriteRule 的目标是 URL 路径还是文件路径。当它以斜线开头时,它假定它是一个 URL 路径并且它是绝对的,因此它将转到文档根目录的 index.php。如果没有斜线,它将猜测文件路径或使用重写库来帮助确定使用哪个。

    您还应该将RewriteBase 指令移到您的规则上方。

    【讨论】:

      【解决方案2】:

      为什么不把基础放在重写器中?

      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ /project/index.php?$1 [L] 
      

      编辑:

      这个问题可能对你有帮助:htaccess RewriteBase

      【讨论】:

        猜你喜欢
        • 2012-05-07
        • 2011-12-27
        • 2014-03-02
        • 1970-01-01
        • 1970-01-01
        • 2011-02-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多