htaccess 将所有图像重定向到处理程序 [英] htaccess redirect all images to handler

查看:59
本文介绍了htaccess 将所有图像重定向到处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是将所有图像(jpeg、png、gifs?)重定向到 index.php?q=file 并且该处理程序将使用 标记.
但恐怕当从该标签调用文件时,它会创建一个无限循环.
所以基本上我要问的是如何仅将外部调用重定向到图像.
(或者有没有更好的方法来保护我的图片?)

What I want to do is redirect all images (jpeg, png, gifs?) to a index.php?q=file and that handler will use a <img src=file> tag.
But I'm afraid that when calling the file from that tag it will create an infinite loop.
So basically what I'm asking is how to redirect only outside calls to an image.
(Or is there a better way to protect my images?)

推荐答案

该规则创建了一个处理程序,它将所有匹配的请求发送到 imageHandler.php

请你试试下面的规则;

RewriteRule ^(.*)(.jpg|.jpeg|.png|.gif|.bmp|.etc)$ imageHandler.php?f=%{REQUEST_FILENAME} [QSA,L]

您将获得 $_GET["f"] 作为请求的文件名和 $_GET["f"] 表示文件在服务器上的路径.

And you will get $_GET["f"] as the file name which requested and the $_GET["f"] represents the path of the file on server.

PS:此规则不会导致您的请求无限循环,只是 imageHandler.php 会执行一些进程,您可以决定是否返回文件.请注意,您必须为 imageHandler.php 实现二进制输出,这可能会导致操作成本.为了降低该操作的成本,您可能需要为 imageHandler.php 实现客户端缓存或服务器缓存机制.

PS: This rule wont cause the infinite loop for your requests just imageHandler.php will execute some processes and you can decide return the file or not. Please mind that you have to implement a binary output for imageHandler.php which can cause cost for your operations. To reduce cost of that operation you may have to implement a client-cache or server-cache mechanism for imageHandler.php.

该规则创建了一个重定向处理程序,它将所有匹配的请求重定向到 index.php

RewriteCond %{HTTP_REFERER} ^$ #That means referer is empty which means direct access to file
RewriteCond %{HTTP_REFERER} !^http://www\.example\.com.* #That means referer is not my domain because when you use a file in <img> tag that will create a request to that image with referer of embedding address
RewriteCond %{REQUEST_URI} !^index.php  #That means request not targeted to index.php (which a part of endless loop)
RewriteCond %{REQUEST_FILENAME} -f #That means request targeted to a file
RewriteCond %{REQUEST_URI} (.jpg|.jpeg|.png|.gif|.bmp)$  #That means request ends with .ext
RewriteRule ^(.*)(.jpg|.jpeg|.png|.gif|.bmp|.etc)$ http://example.com/index.php?f=$1 [R=302,L,QSA] #That means redirect request to index.php?f=file

我没有机会测试第二个,因为我没有 linux 环境但是生产环境,并且无法在生产环境中进行测试.

I do not have a chance to test second one due I do not have a linux environment but production and its not possible to test that in production environment.

希望这些信息对您的情况有所帮助.

Hope these information helps you about your situation.

这篇关于htaccess 将所有图像重定向到处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆