Our Help Portal

Troubleshooting Support Articles
Searchable Help Information Repository
Self-Service Resources How-Tos
Technical Customer Walkthroughs   Browse All Knowledge ! ....

Get Prices Learn More

Breaking

< All Topics
Print

How to prevent image hotlinking a .htaccess?

Preventing image hotlinking (also known as hotlink protection or bandwidth theft) can be done using the .htaccess file on an Apache server. Hotlinking occurs when other websites directly link to the images on your server, using your bandwidth without your permission. To prevent hotlinking, you can add specific rules to your .htaccess file.

Here’s an example of how to prevent image hotlinking using the .htaccess file:

apache
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]
</IfModule>

Replace yourdomain.com with your actual domain. This code checks the referrer (HTTP_REFERER) of incoming requests. If the referrer is not empty and not your own domain, it blocks requests for JPG, JPEG, PNG, and GIF files, returning a forbidden error (403).

Explanation of the code:

  • RewriteCond %{HTTP_REFERER} !^$: Ensures that the referrer is not empty.
  • RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]: Allows requests from your own domain (you need to replace yourdomain.com with your actual domain), and the [NC] flag makes the comparison case-insensitive.
  • RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]: Blocks requests for files with the specified extensions, returning a forbidden error (403). The [L] flag ensures that no further rules are processed for this request.

Important Notes:

  • Make sure to replace yourdomain.com with your actual domain.
  • This method may not be foolproof, as some users might use methods to bypass referrer checks.
  • Hotlink protection may impact legitimate uses like social media embedding. Be cautious and test thoroughly.
  • Always make a backup of your .htaccess file before making changes.

By implementing this code in your .htaccess file, you can help prevent hotlinking of images from external websites. Adjust the file extensions or customize the rules based on your specific needs.

Categories