Pau*_*xon 7 mod-rewrite apache-2.2
我在测试是否%{REQUEST_FILENAME}
存在的 mod_rewrite RewriteCond 条目中的文件测试操作有问题。似乎%{REQUEST_FILENAME}
我得到的不是一条绝对路径,而是一条根植于 the 的路径DocumentRoot
。
我<VirtualHost>
在我的 apache 2.2.9 配置中的一个块中有这个:
RewriteEngine on
RewriteLog /tmp/rewrite.log
RewriteLogLevel 5
#push virtually everything through our dispatcher script
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]*)/?([^/]*) /dispatch.php?_c=$1&_m=$2 [qsa,L]
Run Code Online (Sandbox Code Playgroud)
该规则是通过脚本路由对不存在的文件或目录的请求的常用习惯用法。问题是,即使文件确实存在,它也会触发。
如果我删除规则,我可以请求普通文件就好了。但是随着规则到位,这些请求被定向到 dispatch.php
这是我在 rewrite.log 中看到的
init rewrite engine with requested uri /test.txt
applying pattern '^/([^/]*)/?([^/]*)' to uri '/test.txt'
RewriteCond: input='/test.txt' pattern='!-f' => matched
RewriteCond: input='/test.txt' pattern='!-d' => matched
rewrite '/test.txt' -> '/dispatch.php?_c=test.txt&_m='
split uri=/dispatch.php?_c=test.txt&_m= -> uri=/dispatch.php, args=_c=test.txt&_m=
local path result: /dispatch.php
prefixed with document_root to /path/to/my/public_html/dispatch.php
go-ahead with /path/to/my/public_html/dispatch.php [OK]
Run Code Online (Sandbox Code Playgroud)
因此,在我看来, REQUEST_FILENAME 被显示为文档根目录的路径,而不是文件系统根目录,这大概是文件测试操作符失败的原因。
任何解决这个问题的指针都感激地收到了......
我通过将文档根明确写入条件来解决这个问题
RewriteCond /path/to/my/public_html%{REQUEST_FILENAME} !-f
RewriteCond /path/to/my/public_html%{REQUEST_FILENAME} !-d
Run Code Online (Sandbox Code Playgroud)