Prevent Hotlinking With A .htaccess File

  • January 20, 2024
  • 0 Comments

Prevent Hotlinking With A .htaccess File

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:

  • Your FTP login credentials and an FTP Client like FileZilla
  • SSH credentials or File Manager access
  • Text Editor

READ ME FIRST

As a configuration file, .htaccess is very powerful.
Even the slightest syntax error (like a missing space) can result in your content not displaying correctly or at all.

Since .htaccess is a hidden system file, please make sure your FTP client is configured to show hidden files.
This is usually an option in the program's preferences/options.

This article is provided as a courtesy.

Installing, configuring, and troubleshooting third-party applications is outside the scope of basic support provided by Starburst Services.


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 public_html 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

How do I redirect my site using a .htaccess file?

askapache.com

Wikipedia: Hot-linking


KB Article Created: 2023-12-14
KB Article Updated: 2024-01-20

How helpful was this article to you?

Posting has been disabled.