.htaccess热链接保护-添加图像异常 [英] .htaccess hotlink protection - add image exception

查看:72
本文介绍了.htaccess热链接保护-添加图像异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://my-site.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://my-site.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.my-site.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.my-site.com$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|)$ http://www.my-site.com/dontsteal.png [R,NC]

但是我在图片目录中有这个.htaccess文件。我不喜欢将 dontsteal.png 文件从 images 中移出,因为文件树变得杂乱无章。如果将其放入,则热链接保护将对其产生影响。如果将.htaccess文件移动到子目录中,则必须更改所有图像 src s ...因此,我需要在文件中添加一个例外 dontsteal.png ,使其位于 images 目录中,不受上述代码的影响。有任何想法吗?

But I have this .htaccess file inside the images diretory. I dislike moving the dontsteal.png file from images because the file tree gets disorganized. If I place it in, the hotlink protection will affect it. If I move the .htaccess file in a sub-level directory I'll have to change all my image srcs... Therefore I need to add an exception to the file dontsteal.png so that it's in the images dir and is not affected by the code above. Any ideas?

推荐答案

您可以改用以下内容:

RewriteEngine on

# Check that the file exists
RewriteCond %{REQUEST_FILENAME} -f

# Check we're not being referred from our own domain
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?my-site\.com [NC]

# Check that the request is not fot the hotlink image
RewriteCond %{REQUEST_URI} !^/images/dontsteal\.png$ [NC]

# If all the above conditions are met, redirect to the hotlink image
RewriteRule \.(jpg|jpeg|png|gif)$ http://my-site.com/images/dontsteal.png [NC,R,L]

在这里,我将您的原始条件压缩为一个,并增加了对HTTPS的支持(始终如果您决定使用它,则非常方便)。主要部分是新条件,如果图像请求(文件名)以 dontsteal.png 结尾,则会跳过重定向。这样,您可以将图像放置在任意位置。

Here, I have compressed your original conditions into one, and added support for HTTPS (always handy for if you decide to use it). The main part is the new condition that skips the redirection if the image request (filename) ends with dontsteal.png. This way, you can put that image wherever you like.

更新:添加了新条件以确保请求的文件确实存在-我们不

Update: Added a new condition to ensure the file requested actually exists - we don't really need to protect an image that doesn't.

这篇关于.htaccess热链接保护-添加图像异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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