Filed Under: ModRewrite

HTACCESS: Prevent Hotlinking by Referrer

5 June 2009 No Comment

Well, sometimes this becomes a REAL problem. As soon as your site becomes popular people will start to hotlink your stuff, copy it, post it wherever they can.
Most likely you will even find your stuff on sites like mininova, piratebay and other torrent sites.
Worst of all, they even direct link to your downloads and images and make your bandwitdh go up dramatically.

So this time, we want to take a closer look on the htaccess file and fight!
Actually, many solutions out there are very very limited. Most people will only explain you how to prevent access to a file/folder completely or their solution does not work at all.

I was looking for a solution that would only allow direct access if the visitor would come from my own site and show up another image if they embed one of mine on their site.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?mymainurl.net [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ http://mymainurl.net/angryman.gif [NC,R,L]
 
SetEnvIfNoCase Referer "^http://(www.)?url1.net" spam_ref=1
SetEnvIfNoCase Referer "^http://(www.)?url2.com" spam_ref=2
SetEnvIfNoCase Referer "^http://(www.)?google.com" spam_ref=3
<filesmatch "(.rar)">
order deny,allow
deny from all
allow from env=spam_ref
</filesmatch>

This is it, the best working solution to prevent hotlinking. It will only give access to vistors from google or your own site, plus show an image of your choice when someone tried to put one of your images on their website.

You will have to modify it a bit. Simply add the file extensions to separated by “|”.
Also change the referrers that are allowed to hotlink “SetEnvIfNoCase Referer”.
Make sure to create an image called “angryman.gif” that will show up on other sites. It should contain your URL and a funny line to make the user enter the URL and leave the current website.







Like our posts? Then subscribe via Mail:

Email:  

Similar Posts:

Socialize:

delicious stumbleupon

Leave your response!

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.com.