Apache - RewriteCond%{REQUEST_FILENAME}! - f

时间:2014-05-14 21:56:26

标签: windows apache mod-rewrite

我有这样的.htaccess。

Options FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^media/([^/]+) download.php?file=$1 [L]
RewriteRule ^json/([^/]+)/([^/]+)/([^/]+) jsonfeed.php?module=$1&method=$2&params=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+) index.php?q=$1,$2,$3,$4 [NC,L]

一切正常,直到我有三个目录的图像。例如

之类的东西
http://localhost/upload/html/.thumbs/2.jpg

使用与此最后一条规则类似的网址。 文件已存在。它没有重写引擎就可以正常加载。 我做错了什么?

1 个答案:

答案 0 :(得分:0)

问题与您使用[L]指令的事实有关。后续规则不考虑存在检查或目录检查是成功还是失败。一种解决方案如下:

Options FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^media/([^/]+) download.php?file=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^json/([^/]+)/([^/]+)/([^/]+) jsonfeed.php?module=$1&method=$2&params=$3 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+) index.php?q=$1,$2,$3,$4 [NC,L]

更紧凑的解决方案是首先处理现有的事情,如下所示:

Options FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]

RewriteRule ^media/([^/]+) download.php?file=$1 [L]
RewriteRule ^json/([^/]+)/([^/]+)/([^/]+) jsonfeed.php?module=$1&method=$2&params=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+) index.php?q=$1,$2,$3,$4 [NC,L]