Support / KnowledgeBase

 
Search the KnowledgeBase Search

Prevent hotlinking with a .htaccess file

  • Applies to: (gs), All (dv)

  • Difficulty: Easy

  • Time needed: 10 minutes

  • Tools needed: FTP client, plain text editor

 
  • Applies to: (gs)
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: FTP client, plain text editor
  • Applies to: All (dv)
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: FTP client, plain text editor

Overview

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.

Instructions

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:

Stop hotlinking

To simply stop hotlinking you can add the following to your .htaccess file. Replace example.com on line 3 with your own domain name.


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

Serve alternate content

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 example.com on lines 3 and 4 with your own domain name.


RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)example.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ http://www.example.com/angryman.gif [R,L]

Resources


User Comments

No visitor comments posted. Post a comment
Fields marked with an asterisk(*) are required. Comment on this article
Fill out the form below if you would like to comment on this article.
 
 
 

(code is not case-sensitive)
 
Submit
 
 

Continue