我在testing是否存在%{REQUEST_FILENAME}的mod_rewrite RewriteCond条目中遇到了文件testing操作的问题。 看来,而不是%{REQUEST_FILENAME}是一个绝对path,我得到一个根源于DocumentRoot的path。
我在我的apache 2.2.9configuration中有一个<VirtualHost>块:
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]
该规则是通过脚本为不存在的文件或目录路由请求的常见惯用语。 麻烦的是,即使文件存在,它也会被触发。
如果我删除规则,我可以请求正常的文件就好了。 但是随着规则的到来,这些请求被定向到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]
所以,在我看来,像REQUEST_FILENAME被呈现为文档根目录的path,而不是文件系统根目录,这可能是文件testing操作符失败的原因。
任何指针解决这个感激地收到…
花了我一段时间找出来,但原因是在mod_rewrite文档中提到:
“ REQUEST_FILENAME如果在引用REQUEST_FILENAME服务器已经确定了这个文件或脚本的完整本地文件系统path,否则,如在虚拟主机上下文中使用时 ,与REQUEST_URI值相同。
这就是为什么它在.htaccess但不在虚拟主机的conf文件中。 (这也是为什么添加DOCUMENT_ROOT解决这个问题的原因)。
我通过将文档根写入条件来解决这个问题
RewriteCond /path/to/my/public_html%{REQUEST_FILENAME} !-f RewriteCond /path/to/my/public_html%{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(/pathinfo/|/pathinfo)(.*)$ RewriteRule .* %{DOCUMENT_ROOT}%{REQUEST_URI} [L]