基于 mod_rewrite 的典型 Apache SPA 配置:
DocumentRoot "${webDir}"
<Directory "${webDir}">
LogLevel rewrite:trace8
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule . index.html [L]
</Directory>
如果我用插入符号替换最后一条规则的正则表达式:
RewriteRule ^ index.html [L]
Apache 出现段错误http://localhost/
(适用于任何非空路径)。错误日志没有可疑之处:
[rewrite:trace3] mod_rewrite.c(493): strip per-dir prefix: /home/user/work/out/ ->
[rewrite:trace3] mod_rewrite.c(493): applying pattern '^' to uri ''
[rewrite:trace4] mod_rewrite.c(493): RewriteCond: input='/home/user/work/out/' pattern='!-f' => matched
[rewrite:trace2] mod_rewrite.c(493): rewrite '' -> 'index.html'
[rewrite:trace3] mod_rewrite.c(493): add per-dir prefix: index.html -> /home/user/work/out/index.html
[rewrite:trace2] mod_rewrite.c(493): strip document_root prefix: /home/user/work/out/index.html -> /index.html
[rewrite:trace1] mod_rewrite.c(493): internal redirect with /index.html [INTERNAL REDIRECT]
带有点的日志(不会导致崩溃)是:
[rewrite:trace3] mod_rewrite.c(493) strip per-dir prefix: /home/user/work/out/ ->
[rewrite:trace3] mod_rewrite.c(493) applying pattern '.' to uri ''
[rewrite:trace1] mod_rewrite.c(493) pass through /home/user/work/out/
[rewrite:trace3] mod_rewrite.c(493) strip per-dir prefix: /home/user/work/out/index.html -> index.html
[rewrite:trace3] mod_rewrite.c(493) applying pattern '.' to uri 'index.html'
[rewrite:trace4] mod_rewrite.c(493) RewriteCond: input='/home/user/work/out/index.html' pattern='!-f' => not-matched
[rewrite:trace1] mod_rewrite.c(493) pass through /home/user/work/out/index.html