Article Diary - General Knowledge Digest

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 on line 3 with your own domain name.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?*$ [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 on lines 3 and 4 with your own domain name.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?*$ [NC]
RewriteRule .(gif|jpg)$ [R,L]
Attached Files
There are no attachments for this article.
Comments (1)
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.
Security Code Security Code
Related Articles RSS Feed
Making A Basic PHP Template System
Viewed 709 times since Tue, Dec 8, 2009
Quick Tables for Showing & Updating DB Data
Viewed 855 times since Sun, Dec 27, 2009
Create Dynamic Tabs with jQuery
Viewed 755 times since Sat, Feb 6, 2010
PHP Quiz Questions and Answers
Viewed 2267 times since Mon, Jan 11, 2010
Create Navigation Menu Easily with jQuery Menu
Viewed 849 times since Thu, Dec 31, 2009
IE6 CSS Fixer - Web-Based IE6 CSS Fix Tool
Viewed 830 times since Thu, Dec 31, 2009
Easy Slider jQuery Plugin - Easy Image or Content Slider
Viewed 1061 times since Tue, Jan 12, 2010
How to prevent access to a specific file on server using htaccess?
Viewed 808 times since Sun, Mar 14, 2010
Ajaxed MySQL Table Editor
Viewed 1031 times since Tue, Oct 13, 2009
Create Glowing, Radioactive Buttons with CSS
Viewed 767 times since Sun, Dec 27, 2009