Ben*_*min 2 mod-rewrite url-rewriting
我想重写的路径/health来/health.php.
我目前有以下.htaccess文件,带有一个包罗万象的规则:
RewriteEngine On
RewriteRule ^.*$ index.php [L]
Run Code Online (Sandbox Code Playgroud)
所以我在全能之前添加了一条新规则:
RewriteEngine On
RewriteRule ^health$ health.php [L]
RewriteRule ^.*$ index.php [L]
Run Code Online (Sandbox Code Playgroud)
但令我惊讶的是,health.php当我/health在浏览器中请求时,它不会重写.
但是,当我删除最后一个(全能)规则时,它按预期工作.
为什么mod_rewrite在将规则与[L]标志匹配后不会停止处理规则?
找到了.我一直以为[L]国旗停止了所有规则的处理,期间.实际上,它只是停止处理当前规则集中的规则,但随后在重写的请求上重新运行整个规则集.
因此,我的catch-all代码终于捕获了它,因为重写日志显示:
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]
Run Code Online (Sandbox Code Playgroud)
在此博客上找到的解决方案涉及在catch-all规则之前设置条件,仅在以前没有处理内部重定向时执行它:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^.*$ index.php [L]
Run Code Online (Sandbox Code Playgroud)