Mughil
Mughil

Reputation: 710

How to protect images from being hotlinked?

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?

Upvotes: 0

Views: 2303

Answers (2)

dft
dft

Reputation: 648

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>

Upvotes: 0

Ankit
Ankit

Reputation: 1510

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

Upvotes: 2

Related Questions