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

发布于 2024-11-14 12:24:35 字数 1144 浏览 1 评论 0原文

因此,我正在为 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]

谢谢如需提前提供的任何帮助!

So I am writing an on-the-fly thumbnail generator for a CMS and I am trying to find the best way to handle requests. Specifically, I need to come up with the apache code for an htaccess file that will take all requests to a thumbnail folder and send them to a thumbnail.php file instead.

Ideally it would only forward you to thumbnail.php if the thumbnail doesn't exist, although checking for file existence within apache is beyond me.

The request URL would look like this:

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

And images would be stored in the thumbnails something like this (or could be the same URL as below if that makes more sense):

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

Here is the closest I've come so far, I assume I put this htaccess in the thumbnails folder:

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]

Thank you for any help provided in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抽个烟儿 2024-11-21 12:24:36

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

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

Not sure what the question is here, but there are a few more filters you should be using to check for file existence...

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
来日方长 2024-11-21 12:24:35

我猜您正在寻找正则表达式:

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

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

I guess you're looking for a regex:

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]

I have not tested it, but ideally a request to

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

should call the script thumbnail.php with it's parameters.

Edited: Fixed some typos and errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文