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 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]
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.
Name
Email
Security Code Security Code
Related Articles RSS Feed
PHP Questions for Interview with Answers
Viewed 1748 times since Wed, Feb 24, 2010
ProFolio - Instantly Create Free Portfolio Website
Viewed 1857 times since Sun, Jan 3, 2010
Coming Soon Template
Viewed 1343 times since Thu, Nov 12, 2009
PHP Script For PDF to HTML Conversion
Viewed 7662 times since Tue, Dec 15, 2009
mbSlider - Highly Customizable Content Slider
Viewed 1269 times since Mon, Jul 26, 2010
SEO (Search Engine Optimization) vs SEM(Search Engine Marketing)
Viewed 1059 times since Mon, Jul 12, 2010
New Tags Introduced in HTML5
Viewed 1663 times since Sat, Apr 3, 2010
Free Android GUI PSD File
Viewed 1670 times since Fri, Dec 4, 2009
jDigiClock - HTC Hero Style jQuery Digital Clock
Viewed 1437 times since Tue, Feb 16, 2010
MENU