How to block the hotlink by referrer

Blocking users or sites that originate from a particular domain is a very useful trick of .htaccess. Lets say you check your logs one day, and see tons of referrals from a particular site, yet upon inspection you can’t find a single visible link to your site on theirs. The referral isn’t a “legitimate” one, with the site most likely hot linking to certain files on your site such as images, .css files,  or files you can’t even make out. Especially those mp3 audio or mp4 video files. They are large files. The site hot link your content will eat a lot of portion of your bandwidth, CPU time. The result is make the normal user of your site loading page slower than normal.

Remember, your logs will generate a referrer entry for any kind of reference to your site that has a traceable origin.

Here I have some example of how to write the .htaccess file for Apache Web Server.

RewriteEngine On
RewriteCond %{HTTP_REFERER} .**$ [OR]
RewriteCond %{HTTP_REFERER} .**$ [OR]
RewriteCond %{HTTP_REFERER} .**$ [OR]
RewriteCond %{HTTP_REFERER} .**$
RewriteRule ^(.*)$ -

Blocked referrers will be shown a ‘403 Forbidden’ error message.