【问题标题】:Rewriting does not stop after matching a RewriteRule with the [L] flag将 RewriteRule 与 [L] 标志匹配后,重写不会停止
【发布时间】:2013-08-05 21:41:58
【问题描述】:

我想将路径/health重写为/health.php

我目前有以下 .htaccess 文件,带有 catch-all 规则:

RewriteEngine On
RewriteRule ^.*$ index.php [L]

所以我只是在包罗万象之前添加了一条新规则:

RewriteEngine On
RewriteRule ^health$ health.php [L]
RewriteRule ^.*$ index.php [L]

但令我惊讶的是,当我在浏览器中请求 /health 时,它并没有重写为 health.php

但是,当我删除最后一个(包罗万象)规则时,它按预期工作。

为什么 mod_rewrite 在匹配带有[L] 标志的规则后不停止处理规则?

【问题讨论】:

    标签: mod-rewrite url-rewriting


    【解决方案1】:

    找到了。我一直认为[L] 标志停止了所有规则的处理,期间。实际上,它只是停止处理当前规则集中的规则,然后根据重写的请求重新运行整个规则集

    因此,我的包罗万象的代码终于捕捉到了它,正如rewrite log 所示:

    strip per-dir prefix: [...]/public/health -> health
    applying pattern '^health$' to uri 'health'
    rewrite 'health' -> 'health.php'
    add per-dir prefix: health.php -> [...]/public/health.php
    strip document_root prefix: [...]/public/health.php -> /health.php
    internal redirect with /health.php [INTERNAL REDIRECT]
    
    strip per-dir prefix: [...]/public/health.php -> health.php
    applying pattern '^health$' to uri 'health.php'
    strip per-dir prefix: [...]/public/health.php -> health.php
    applying pattern '^.*$' to uri 'health.php'
    rewrite 'health.php' -> 'index.php'
    add per-dir prefix: index.php -> [...]/public/index.php
    strip document_root prefix: [...]/public/index.php -> /index.php
    internal redirect with /index.php [INTERNAL REDIRECT]
    
    strip per-dir prefix: [...]/public/index.php -> index.php
    applying pattern '^health$' to uri 'index.php'
    strip per-dir prefix: [...]/public/index.php -> index.php
    applying pattern '^.*$' to uri 'index.php'
    rewrite 'index.php' -> 'index.php'
    add per-dir prefix: index.php -> [...]/public/index.php
    initial URL equal rewritten URL: [...]/public/index.php [IGNORING REWRITE]
    

    this blog 上找到的解决方案涉及在 catch-all 规则之前放置一个条件,以便仅在之前没有处理过内部重定向时执行它:

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^.*$ index.php [L]
    

    【讨论】:

      猜你喜欢
      • 2011-10-11
      • 1970-01-01
      • 2019-12-06
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      相关资源
      最近更新 更多