我想将路径/health
重写为/health.php
。
我目前拥有以下.htaccess
文件,其中包含 catch-all 规则:
RewriteEngine On
RewriteRule ^.*$ index.php [L]
所以我刚刚在全能之前添加了一条新规则:
RewriteEngine On
RewriteRule ^health$ health.php [L]
RewriteRule ^.*$ index.php [L]
但令我惊讶的是,当我在浏览器中请求health.php
时,它不会重写为/health
。
但是,当我删除最后一个(全能)规则时,它会按预期工作。
为什么mod_rewrite在将规则与[L]
标志匹配后不会停止处理规则?
答案 0 :(得分:5)
找到它。我一直认为[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]