htaccess:无法检查dynamic文件path的存在!

我已经写了一个dynamic的缩略图创build者的htaccess文件,看看是否存在请求的文件,如果不发送用户到PHP页面来创build它。 我的htaccess文件如下所示:

# Check the formatting (this works) RewriteCond %{SCRIPT_FILENAME}%{QUERY_STRING} /([a-zA-Z0-9-]+).(jpg|gif|png)w=([0-9]+)&h=([0-9]+)(&c=(true|false)) # Only forward to thumbnail.php if file doesn't exist #RewriteCond %{REQUEST_FILENAME} !-f #this works but not for the correct filename! RewriteCond %1-%2-%3-%4-%6.jpg !-f # Doesn't work!!! # If file doesn't exist, send here: (this works) RewriteRule ^.*$ thumbnail.php?file_name=%1&type=%2&w=%3&h=%4&c=%6 

我检查文件的存在似乎不工作,但是…使用%{REQUEST_FILENAME}的旧版本的作品,但这不是我需要检查的正确的文件名。 我已经validation了新文件%1-%2-%3-%4-%6.jpg具有正确的输出,但不触发条件!

基于以下url:

 this-is-the-image-name.gif?w=200&h=100&c=true 

htaccess文件应该检查​​这个文件是否存在:

 this-is-the-image-name-gif-200-100-true.jpg 

缩略图和htaccess文件都在同一个文件夹中,但是这个文件夹是未知的,不能被硬编码。 它是否需要完整的path来检查? 如果是这样,我怎么能得到完整的path,但我的自定义文件名?

用这个拉我的头发… 帮助! 谢谢。

你完全错了(恕我直言)。 这应该为你做的工作。

 RewriteCond %{QUERY_STRING} ^w=([0-9]+)&h=([0-9]+)(&c=(true|false)) RewriteCond %{DOCUMENT_ROOT}/$1-$2-%1-%2-%4.jpg !-f RewriteRule ^([a-z0-9\-]+)\.(jpg|jpeg|gif|png)$ thumbnail.php?file_name=$1&type=$2&w=%1&h=%2&c=%4 [NC] 

1)我已经testing过它,它可以工作,但我可能会错过一些只适用于您的设置/系统的东西。

2)假定缩略图图像(例如this-is-the-image-name-gif-200-100-true.jpg )位于网站的根文件夹中(例如,如果网站位于/var/www/http那么它检查以下文件: /var/www/http/this-is-the-image-name-gif-200-100-true.jpg

3)如果您的缩略图图像位于/thumbnails子文件夹中,则需要对#2和#3行进行一些小的更改。

UPDATE

我看到你原来的问题: https : //stackoverflow.com/questions/6302529/htaccess-cant-check-dynamic-file-existence

根据您的笔记,生成的缩略图将与thumbnail.php一起存储在/media/thumbnails/文件夹中。 基于这些信息,规则可以按照以下方式重写:

 RewriteCond %{QUERY_STRING} ^w=([0-9]+)&h=([0-9]+)&c=(true|false) RewriteCond %{DOCUMENT_ROOT}/$1/$2-$3-%1-%2-%3.jpg !-f RewriteRule ^(media/thumbnails)/([a-z0-9\-]+)\.(jpg|jpeg|gif|png)$ /$1/thumbnail.php?file_name=$2&type=$3&w=%1&h=%2&c=%3 [NC] 

这些规则需要放在网站的根.htaccess文件中。

你仍然可以玩,使其独立于文件夹名称(我想你可能需要插入另一个RewriteCondition之后第一个检查相对path%{REQUEST_URI}),但是我很难玩虚拟文件没有做好基础设施准备。