使用.htaccess和mod_rewrite防止图像热链接

One way to kill your website's bandwidth and overall download speed is to not block image "hotlinking." What's hotlinking? Hotlinking is linking to a file on an external server that the Web Developer does not own (most of the time the Web Developer doesn't even have permission to use the file). Hot linking occurs mostly with images.

杀死网站带宽和整体下载速度的一种方法是不阻止图像“热链接”。 什么是热链接? 热链接是链接到Web开发人员不拥有的外部服务器上的文件(大多数情况下,Web开发人员甚至没有使用该文件的权限) 。 热链接主要发生在图像上。

Why would another website hotlink to a file on your server? There are a variety of reasons, both innocent and evil:

为什么另一个网站热链接到您服务器上的文件? 有多种原因,包括无辜和邪恶:

  • To use up the other website's bandwidth / save your bandwidth (evil)

    耗尽另一个网站的带宽/保存您的带宽(邪恶)
  • To link to an oft-changing file so that the file is always up to date on your website (mostly innocent)

    链接到经常更改的文件,以便该文件在您的网站上始终是最新的(大多数情况是无害的)
  • Laziness (both)

    懒惰(两者)

How can this be prevented? Relatively easily using some quick .htaccess directives.

如何预防? 使用一些快速的.htaccess指令相对容易。

代码 (The Code)

The following prevents any domain beside yours from hotlinking:

以下内容可防止您旁边的任何域进行热链接:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?yourwebdomain.com(/)?.*$ [NC]

The following allows only a friend to hotlink -- everyone else is barred:

以下内容仅允许朋友进行热链接-禁止其他所有人:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?yourwebdomain.com(/)?.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www.)?friend1domain.com(/)?.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www.)?friend2domain.com(/)?.*$ [NC]

The following allows for protection of only specified file extensions:

以下仅允许保护指定的文件扩展名:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?yourwebdomain.com(/)?.*$ [NC]
RewriteRule .*.(gif|jpe?g)$ [F,NC]

The following returns a "stop stealing my images" image:

以下返回“停止窃取我的图像”图像:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?yourwebdomain.com(/)?.*$ [NC]
RewriteRule .(gif|jpe?g|png|bmp)$ /graphics/stop-stealing.jpg [L,NC]

You can use the above coding practices to prevents thievery of any type of file you'd like, including CSS, JavaScript, and text files.

您可以使用上述编码方法来防止任何类型的文件,包括CSS,JavaScript和文本文件。

翻译自: https://davidwalsh.name/prevent-image-hotlinking

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值