Prevent hotlinking with apache

To allow all domain except one:

RewriteCond %{HTTP_REFERER} ^http://(www\.)?offendinddomain\.com/$ [NC]
RewriteRule \.(gif|jpg|png)$ http://example.com/hotlink.png [R,L]

This will prevent holinking from the domain offendingdomain.com and serve the image from example.com instead.

Prevent more than one domain:

RewriteCond %{HTTP_REFERER} ^http://(www\.)?offendinddomain\.com/$ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?offendinddomain2\.com/ [NC]
RewriteRule \.(gif|jpg|png)$ http://example.com/hotlink.png [R,L]

This will prevent hotlinking from the 2 offendind domains, if you need more just duplicate the second line.

Block all:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?yourdomain\.com/.*$ [NC]
RewriteRule .*\.(gif|jpe?g|png)$ http://example.com/hotlink.png [R,NC,L]

This will block all hotlinking and and serve the image from example.com instead.

 

NOTE: Be carefull not to rewrite to a image in the same domain as yours or you could create a redirect loop.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>