1

我正在为 CMS 编写动态缩略图创建器,需要动态检查文件是否存在。我目前已经创建了一个 htaccess 文件,用于检查您请求的文件是否存在,但需要它更高级一些,并根据提交的 URL 选项“创建”文件检查。

这是我目前拥有的(基于图像 ID,而不是名称):

RewriteEngine on

#Check for file existence here and forward if exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)-([0-9]+)-([0-9]+)(-(crop))?\.jpg$ thumbnail.php?id=$1&w=$2&h=$3&c=$4 [L,QSA]

基于此网址:

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

htaccess 文件检查此文件是否存在:

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

如果它不存在,则 RewriteRules 用户可以:

thumbnail.php?name=this-is-the-image-name&type=gif&w=200&height=100&c=true

"Crop" 是可选的,所以没有它之前的 URL 看起来像这样:

this-is-the-image-name.gif?w=200&h=100
this-is-the-image-name-gif-200-100.jpg
thumbnail.php?name=this-is-the-image-name&type=gif&w=200&height=100

基本上,我需要 RewriteCond 根据它基于REQUEST_FILENAME. 有任何想法吗?

像这样的东西:

RewriteCond ^([a-zA-Z0-9-]+).(jpg|gif|png)?w=([0-9]+)&h=([0-9]+)(&c=(-crop))?\.jpg$ %$1-$2-$3-$4-$6.jpg !-f

甚至不确定这是否可能......在这种情况下,我会让它转发所有请求到 PHP 文件,但由于 PHP 有很多开销,我认为这会更快。

非常感谢您提前提供的任何帮助!

4

1 回答 1

3

来自 rewritecond 文档

RewriteRule 反向引用:这些是 $N (0 <= N <= 9) 形式的反向引用,它提供对模式的分组部分(在括号中)的访问,从 RewriteRule 受当前 RewriteCond 条件集的约束。 .

RewriteCond 反向引用:这些是 %N (1 <= N <= 9) 形式的反向引用,它提供从当前条件集中最后匹配的 RewriteCond 访问模式的分组部分(同样,在括号中)。

# file is a gif
RewriteCond %{REQUEST_FILENAME} .gif$

# capture the and params in () backreferences
RewriteCond %{QUERY_STRING} w=(200)&h=(100)&c=true

# only rewrites for files that do not exist
RewriteCond %{REQUEST_FILENAME}-gif-%1-%2-crop.jpg !-f

# this rewrites not found thumbs to your php script
RewriteRule %.*$ thumbnail.php?name=%{REQUEST_FILENAME}&type=gif&w=%1&height=%2&c=true
于 2011-06-09T19:25:27.797 回答