【问题标题】:mod_rewrite rules for existing files现有文件的 mod_rewrite 规则
【发布时间】:2011-12-11 01:46:40
【问题描述】:

我正在定义 mod_rewrite 规则,如果请求的文件不存在,它将重写我的 /application.php 的所有请求,否则不会进行任何重写。很简单:

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule .* application.php [PT]

代码只有一个问题。假设我有 foo.html 文件。然后请求如下:

http://example.com/foo.html/some/other/string

会出现 404 错误。 为什么?

【问题讨论】:

    标签: apache mod-rewrite


    【解决方案1】:

    会出现 404 错误。为什么?

    因为该 URL 不存在。它正在文件夹/foo.html/some/other 中查找文件string,但它不存在。

    您希望使用http://example.com/foo.html/some/other/string URL 结构来利用的行为——将第一个条目视为文件名,将其余条目视为它的参数——称为"pathinfo"。它与 mod_rewrite 无关,但如果您在 Apache 配置中启用以下内容,它将可用:

     AcceptPathInfo On
    

    该设置目前似乎已为您“关闭”。

    如果你启用它,文件名后面的部分将提供给foo.html - 在PHP中,它将在

    $_SERVER["PATH_INFO"]
    

    变量。

    因为这种方法不需要激活重写模块,这有时被称为“穷人的 mod_rewrite”——它工作得很好,但不如“真正的”重写那么灵活。

    【讨论】:

      猜你喜欢
      • 2011-03-26
      • 1970-01-01
      • 2012-07-23
      • 2011-07-01
      • 2018-02-03
      • 1970-01-01
      • 2011-02-27
      • 2010-09-17
      相关资源
      最近更新 更多