Home » Categories » Business » Web Development

Prevent hotlinking with a .htaccess file

Hotlinking refers to linking directly to non-html objects on other servers, such as images, movie files etc. This can greatly impact bandwidth usage, and in some cases GPUs if you are on the (gs) Grid-Service. You can prevent these requests on your server using a .htaccess file.

If you already have an .htaccess file simply add one of these two snippets of code to your existing file, or create a new plain text document with this code and upload it to your domains directory, or a particular sub directory to localize the effect to just one of your sites, or section of one of these sites:

To simply stop hotlinking you can add the following to your .htaccess file:

Replace mt-example.com on line 3 with your own domain name.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?mt-example.com/.*$ [NC]
RewriteRule .(gif|jpg|jpeg|bmp|zip|rar|mp3|flv|swf|xml|php|png|css|pdf)$ - [F]

To serve alternate content when hotlinking is detected:

You can set up your .htaccess file to actually display different content when hotlinking is attempted. This is more commonly done with images suggesting your displeasure of this activity, such as serving up an "Angry Man" image in place of the hotlinked one. Once again replace mt-example.com on lines 3 and 4 with your own domain name.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?mt-example.com/.*$ [NC]
RewriteRule .(gif|jpg)$ http://www.mt-example.com/angryman.gif [R,L]
Attachments Attachments
There are no attachments for this article.
Comments (1) Comments
Comment by Rahul Ghatotkach on Thu, Oct 8th, 2009 at 12:55 PM
Its is very helping article here about Preventing hotlinking with a .htaccess file. Keep it up. Its is nice one.
Related Articles RSS Feed
Web Application Security Tools & Scanner Applications
Viewed 1544 times since Mon, Mar 22, 2010
Geo-location Mapping With Google Maps API & PHP
Viewed 1718 times since Mon, Apr 4, 2011
30 High Quality Charts & Graphs for Web Developers
Viewed 3249 times since Wed, Mar 23, 2011
Search Engine Optimization: Tags of Importance for SEO
Viewed 390 times since Tue, Oct 12, 2010
tESTR
Viewed 882 times since Wed, Aug 10, 2011
Making A Basic PHP Template System
Viewed 1353 times since Tue, Dec 8, 2009
TipTip - jQuery Tooltip Plugin
Viewed 1051 times since Thu, Jan 28, 2010
How to redirect an old domain to a new domain using htaccess?
Viewed 446 times since Sun, Mar 14, 2010