0

我在 Apache (2.2) 配置的虚拟主机部分有以下简单的重写语句和条件

RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^([^\.]{2,})$ /bootstrap.php?url=$1 

所有请求都很好地重定向到 bootstrap.php,如果文件存在则执行。这很好用。但是,如果存在目录,我希望 Apache 为该目录运行 DirectoryIndex,或显示目录的内容(无论我选择什么设置),但 Apache 也将请求定向到 bootstrap.php。似乎第一个 rewriteCond 不正确。我究竟做错了什么?

在进行调试并尝试加载“localhost/img”(存在)时,我得到以下输出

127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (2) init rewrite engine with requested uri /img/
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (3) applying pattern '^([^\.]{2,})$' to uri '/img/'
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (4) RewriteCond: input='/img/' pattern='!-d' => matched
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (4) RewriteCond: input='/img/' pattern='!-f' => matched
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (2) rewrite '/img/' -> '/bootstrap.php?url=/img/'
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (3) split uri=/bootstrap.php?url=/img/ -> uri=/bootstrap.php, args=url=/img/
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (2) local path result: /bootstrap.php
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (2) prefixed with document_root to C:/webroot/zippyshoot/bootstrap.php
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (1) go-ahead with C:/webroot/zippyshoot/bootstrap.php [OK]

加载 localhost/img/logo.png 时工作正常。然后是调试日志的输出

127.0.0.1 - - [14/Jan/2013:20:27:56 +0100] [localhost/sid#4cd5a8][rid#237d978/initial] (2) init rewrite engine with requested uri /img/logo.png
127.0.0.1 - - [14/Jan/2013:20:27:56 +0100] [localhost/sid#4cd5a8][rid#237d978/initial] (3) applying pattern '^([^\.]{2,})$' to uri '/img/logo.png'
127.0.0.1 - - [14/Jan/2013:20:27:56 +0100] [localhost/sid#4cd5a8][rid#237d978/initial] (1) pass through /img/logo.png

我究竟做错了什么?

4

1 回答 1

1

我在这篇文章http://amandine.aupetit.info/135/apache2-mod_rewrite/中找到了问题的答案

从 Apache 2.2 开始,rewriteCond 发生了变化。现在写它们的正确方法是

     RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
     RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
     RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-l   
于 2013-01-15T19:17:00.613 回答