How to protect images from being hotlinked?

蹲街弑〆低调 提交于 2019-12-12 00:32:21

问题


I have tried the below code in htaccess to stop my images from being hotlinked, but it is not working.

SetEnvIfNoCase Referer "^http://www.example.com/" locally_linked=1
SetEnvIfNoCase Referer "^http://www.example.comm$" locally_linked=1
SetEnvIfNoCase Referer "^http://example.com/" locally_linked=1
SetEnvIfNoCase Referer "^http://example.com$" locally_linked=1
SetEnvIfNoCase Referer "^$" locally_linked=1
<FilesMatch "\.(gif|png|jpe?g|css|js)$">
  Order Allow,Deny
  Allow from env=locally_linked=1
</FilesMatch>

Can anyone help me with how to prevent hotlinking?


回答1:


This will show a fixed image whenever its hotlinked. (That image may have a message that Hotlinking in not allowed...)

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com [NC]
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]

It checks Refererr is not nothing, and referer is not matching mydomain then it responds with the image nohotlink.jpg.

To understand hotlinking prevention better see these SO threads:
Apache .htaccess hotlinking redirect
Apache Hotlink Protection for Download Folder
A Basic tutorial http://altlab.com/htaccess_tutorial.html




回答2:


This disallows anything from hotlinking unless the referer is from example.com.

SetEnvIf Referer example\.com localreferer
<FilesMatch \.(jpg|png|gif|css|js|pdf|doc|xls|txt)$>
    Order deny,allow
    Deny from all
    Allow from env=localreferer
</FilesMatch>


来源:https://stackoverflow.com/questions/12324226/how-to-protect-images-from-being-hotlinked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!