Hotlink protection is a technique used to prevent other websites from linking to your images and other media files, thus stealing your bandwidth and slowing down your website. One way to implement hotlink protection is by using the .htaccess file on an Apache web server.
Here is an example of an .htaccess code that will prevent hotlinking of images on your site:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
This code uses the mod_rewrite module of Apache to check the HTTP referer (the website that is linking to your images), and if it is not your own website, the request will be denied and the image will not be displayed.
You can replace yourdomain.com with your own domain name and also you can add or remove the extensions you want to protect. The code above will only block jpg, jpeg, png and gif files, however you can add or remove file extensions as per your requirement.
You should be careful with the use of .htaccess files as it can cause errors or security issues if not properly configured. Another approach is using the CDN (Content Delivery Network) which generally provides this feature as well.
Leave a Reply