Support / KnowledgeBase

 
Search the KnowledgeBase Search

Prevent hotlinking with a .htaccess file

  • Applies to: All DV, Grid

  • Difficulty: Easy

  • Time needed: 10 minutes

  • Tools needed: FTP client, plain text editor

 
  • Applies to: Grid
    • Difficulty: Medium
    • Time Needed: 15
    • 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 Grid. You can prevent these requests on your server using a .htaccess file.

Requirements

Before you start, be sure to have handy:

READ ME FIRST

This article is provided as a courtesy. Installing, configuring, and troubleshooting third-party applications is outside the scope of support provided by (mt) Media Temple. Please take a moment to review the Statement of Support.

READ ME FIRST

The publishing of this information does not imply support of this article. This article is provided solely as a courtesy to our customers. Please take a moment to review the Statement of Support.

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

 

Continue