Prevent hotlinking with a .htaccess file


Browse by products and services

  • Applies to: Grid
    • Difficulty: Medium
    • Time Needed: 15
    • Tools Required: FTP client, SSH access, or File Manager access, plain text editor
  • Applies to: Legacy DV & VPS Hosting
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: FTP client, SSH access, or File Manager access, 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 Grid. You can prevent these requests on your server using a .htaccess file.

Requirements

Before you start, be sure to have handy:

STATEMENT OF SUPPORT:
Please keep in mind that troubleshooting the configuration/functionality of third-party applications is not covered by our statement of support. These resources were provided as a courtesy to assist you to the extent of our abilities. For more information on our statement of support, feel free to click here.

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