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?)
htaccess将所有图像重定向到处理程序
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- douxian7808 2016-01-27 13:59关注
That rule creates a handler which takes all matching requests to imageHandler.php
Please can you try the rule below;
RewriteRule ^(.*)(.jpg|.jpeg|.png|.gif|.bmp|.etc)$ imageHandler.php?f=%{REQUEST_FILENAME} [QSA,L]
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: 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.
That rule creates a redirect handler which redirects all matching requests to 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
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.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 用twincat控制!
- ¥15 请问一下这个运行结果是怎么来的
- ¥15 单通道放大电路的工作原理
- ¥30 YOLO检测微调结果p为1
- ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
- ¥15 DS18B20内部ADC模数转换器
- ¥15 做个有关计算的小程序
- ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
- ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
- ¥15 setInterval 页面闪烁,怎么解决