【问题标题】:htaccess: specific directory requests to forward to PHP filehtaccess:特定目录请求转发到 PHP 文件
【发布时间】:2011-09-11 01:37:26
【问题描述】:

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

理想情况下,如果缩略图不存在,它会将您转发到 thumbnail.php,尽管我无法在 apache 中检查文件是否存在。

请求 URL 如下所示:

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]

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

【问题讨论】:

  • 你所拥有的有什么问题?你期待什么行为?您实际看到的是什么行为?

标签: php apache .htaccess redirect thumbnails


【解决方案1】:

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

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

【讨论】:

    【解决方案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

    已编辑:修正了一些错别字和错误。

    【讨论】:

    • 谢谢,这正是我想要的。但是,当我尝试访问图像时,它会出现内部服务器错误 (500):http://getdirectus.com/dev/media/thumbnails/test-200-100-true.jpg
    • 我认为是 .htaccess 文件。检查您的网络服务器的错误日志,它应该说明哪一行有错误。还会显示正则表达式错误。
    • dot . 需要转义为 \. 并且标志应该是 [L,QSA] 最后。
    • 我在 .htaccess 演示中遇到了一些错误,已更新。如果您还有 500 人,请检查您的错误日志,因为我无法完全模仿您的系统。
    • @cbh:我更正了[a-z-]+ 加号,这是错误之一并通过了。 “任何文件名”代表什么?你具体在找什么?某些特定的字符?如果有,有哪些?还是最后的结尾(.jpg、.png 等)?
    猜你喜欢
    • 2014-10-17
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多