防止生成的缩略图的热链接?

时间:2011-10-04 13:10:02

标签: .htaccess mod-rewrite

我有一个.htaccess文件,其中包含防止静态资产(图像,CSS和JavaScript文件)热链接的指令。相关代码如下所示:

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?ipwuk.com/.*$ [NC]
RewriteRule \.(gif|jpg|js|css|png)$ - [F,L]

这很棒,做得很好;但是,我有一个自动生成缩略图的PHP脚本(并且不包含URL中的文件扩展名)。示例缩略图网址如下所示:

  

http://example.com/inc/image.php/4e7efde2ee8ac.jpg/610/343/fill

或者:

  

http://example.com/inc/image.php/ [seed]。[extension] / [width] / [height] / [thumb type]

如何防止上述脚本生成的图像被热链接(我怀疑我的客户正在做什么,结果是每天超过150MB的带宽?)

2 个答案:

答案 0 :(得分:1)

你真的不能阻止 - 编辑:劝阻 - 它会在生成缩略图的脚本中出现吗?

在伪PHP中:

if (parse_url($_SERVER['HTTP_REFERER']) != "yourSite"){

   //generate thumbnail that says "don't hotlink, scum!"

}
else {

    //do your normal thumbnail generation routine

} 

答案 1 :(得分:1)

此代码拒绝所有非直接或来自指定网站的image.php请求。

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?ipwuk\.com/.*$ [NC]
RewriteCond %{REQUEST_URI}  ^/image.php(.*)$ [NC]
RewriteRule ^(.*)$ - [F,L]
相关问题