【问题标题】:Mod_rewrite not Behaving as ExpectedMod_rewrite 未按预期运行
【发布时间】:2012-09-21 15:54:17
【问题描述】:

我想创建一个重写规则,以便

www.example.com/whatever/here

将映射到

www.example.com/index.php/whatever/here

这样$_SERVER['PATH_INFO'] 就可以使用了。为此,我创建了以下规则集:

RewriteEngine On
RewriteCond {%REQUEST_FILENAME} !-f
RewriteCond {%REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1

只有这会给我一个 500 服务器错误。 Apache 错误日志指出

[2012 年 9 月 30 日星期日 17:23:08.758705] [核心:错误] [pid 2584:tid 1704] [客户端 ::1:58591] AH00124:请求超出了 10 个内部的限制 由于可能的配置错误而重定向。采用 'LimitInternalRecursion' 必要时增加限制。采用 'LogLevel debug' 获取回溯。

但我看不到递归。当我尝试它时,让我们说一个查询:

RewriteRule ^(.*)$ index.php?route=$1

它按预期工作,但$_SERVER['PATH_INFO'] 不可用。


另外,在有人问之前,我想要$_SERVER['PATH_INFO'],因为这样我就可以拥有像这样的复杂路径:

www.example.com/controller/action/data?more=data

如果我需要的话。

【问题讨论】:

    标签: apache mod-rewrite


    【解决方案1】:

    您的服务器环境变量在RewriteCond 中指定不正确,如果那是您的真实代码。如果不是一个完全 500 的解析错误,这将导致条件被忽略,并在每个请求上进行重写,从而导致循环。

    RewriteEngine On
    # The % goes outside the {}
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # And add a [L] flag
    RewriteRule ^(.*)$ index.php/$1 [L]
    

    【讨论】:

    • 完美,我知道这很愚蠢,这很好用。非常感谢:)
    猜你喜欢
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 2010-09-27
    • 2016-03-31
    • 2014-11-12
    • 1970-01-01
    • 2020-06-28
    • 2018-01-18
    相关资源
    最近更新 更多