2

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

我目前有以下.htaccess文件,其中包含一个包罗万象的规则:

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

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

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

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

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

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

4

1 回答 1

5

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

因此,我的包罗万象的代码终于捕捉到了它,正如重写日志所示:

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]

在此博客上找到的解决方案涉及在 catch-all 规则之前放置一个条件,以便仅在之前未处理任何内部重定向时执行它:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^.*$ index.php [L]
于 2013-08-05T22:54:26.750 回答