htaccess:特定目录请求转发到PHP文件

时间:2011-06-08 17:02:12

标签: php apache .htaccess redirect thumbnails

所以我正在为CMS编写一个动态缩略图生成器,我正在尝试找到处理请求的最佳方法。具体来说,我需要为htaccess文件提供apache代码,该文件将所有请求发送到缩略图文件夹并将其发送到thumbnail.php文件。

理想情况下,如果缩略图不存在,只会将你转发到thumbnail.php,尽管在apache中检查文件是否存在超出了我。

请求网址如下所示:

http://unknown_domain.com/unknown_folder/media/thumbnails/image-name.jpg?w=200&h=100&c=true

图像将存储在缩略图中(如果更有意义,可以是与下面相同的URL):

http://unknown_domain.com/unknown_folder/media/thumbnails/image-name-200-100-true.jpg

这是我到目前为止最接近的,我假设我把这个htaccess放在缩略图文件夹中:

RewriteEngine on
#RewriteBase /unknown_folder/media/thumbnails/ # optional, depends on your setup

#Check for file existence here and forward if exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z-])-([0-9]+)-([0-9]+)-true.jpg$ thumbnail.php?w=$2&h=$2&c=true [LQSE]

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

2 个答案:

答案 0 :(得分:2)

我猜你正在寻找一个正则表达式:

RewriteEngine on
RewriteBase /unknown_folder/media/thumbnails/ # optional, depends on your setup

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

我没有测试过,但理想情况下是

的请求
/unknown_folder/media/thumbnails/bla-you-name-of-whatever-200-100-true.jpg

应该使用它的参数调用脚本thumbnail.php

已编辑:修正了一些拼写错误和错误。

答案 1 :(得分:0)

不确定这里的问题是什么,但是您应该使用更多的过滤器来检查文件是否存在...

RewriteEngine on

#Check for file existence here and forward if exists
RewriteCond ^/(regex_here)/$ !-f
RewriteCond ^/(regex_here)/$ !-d
RewriteCond ^/(regex_here)/$ !-s
RewriteCond ^/(regex_here)/$ !-l

RewriteRule ^/(regex_here)/$ /thumbnail.php?w=$1&h=$2&c=$3