By
Rayhan Arif
October 14, 2019
Share
Hotlinking is showcasing someone else’s media file (image, video, audio, etc.) on your site with the direct link to where they have been hosted. It’s a controversial practice that may be regarded as unethical and can be illegal in some cases. Hotlinking can cause a huge drain on the bandwidth of the source site. Whether you hotlink people’s images or not, there are some guys out there to do it to your site. If you want to save your resources, you must prevent image hotlinking.
In WordPress, there are several ways you can serve media to your site. Delivering images with links is a feature which is being misused by the bandwidth thieves. No worries, there are ways to prevent image hotlinking in WordPress and this is what we will talk about today.
Hotlinking is a process of serving images or other media files by using a direct link to other website’s servers. It’s like you find an image on a website and you use the image link to showcase on your site. Images are being displayed on your site but they are being served from a different location. It’s unfair and unjust on the host sites unless you have explicit permission to do so.
I would ask why shouldn’t you. Hotlinking will eat out your bandwidth without doing anything good for your website. You may have to pay extra bucks for the bandwidth of the traffic that is not yours. You should protect your website media files from hotlinking for the following reasons.
If in any case, you serve people’s images on your site via hotlinking, the images can be gone if the host site makes any changes to the link or take any measure to prevent hotlinking. So, hotlinking not only affects the host site but also affects the serving site. This is why you should avoid hotlinking too.
You cannot detect hotlinking unless you conduct research. When you are experiencing a poor website performance, you should look into whether it is caused by hotlinking. You can detect image hotlinking by using google images.
Search on Google with the following command and click on images
inurl:yourwebsite.com -site:yourwebsite.com
If you want to check hotlinked images of Themeum.com, you should search Google with the command below and click on Images.
inurl:themeum.com -site:themeum.com
The search engine will display hotlinked images along with the sites.
Whether any of your website media resources have been hotlinked or not, you can take preventive measures at any time. It’s quite easy to prevent hotlinking with the process we show you here. Follow any of the ways below to prevent hotlinking in WordPress sites. Details are followed by the sneak peak.
You can prevent hotlinking by writing a simple code snippet in the .htaccess file of your Apache webserver. Copy the code snippet below and paste it on your website’s .htaccess file.
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
Prevent Image Hotlinking in NGINX server
Copy the code snippet below and paste it on your NGINX config file.
location ~ .(gif|png|jpeg|jpg|svg)$ {
valid_referers none blocked ~.google. ~.bing. ~.yahoo. yourdomain.com *.yourdomain.com;
if ($invalid_referer) {
return 403;
}
}
If you use any other webserver please check their documentation.
Some CDN providers have a pre-built image hotlink protection system. Services like KeyCDN and Cloudflare have great built-in hotlink protection for free that you can enable easily. If you are using any of such CDN, check whether you have hotlink protection system in them. Enable hotlink protection if your CDN has this feature.
If your host uses cPanel on your site’s server, you can use their hotlink protection tool as well. Go to your cPanel and scroll down to Hotlink Protection and click on it.
Click on Enable to activate hotlink protection.
Take a look at the cPanel documentation to know how to enable it in the setting.
There are a few WordPress plugins like All In One WP Security And Firewall having the ability to prevent or take measures against hotlinking. For example, the built-in system in All In One WP Security And Firewall plugin helps you prevent hotlinking in an easy way.
We’ve shown 4 different ways to prevent hotlinking in WordPress sites. You can follow any of the ways you prefer. You should be careful about your website resources and protect them by any means possible to ensure better performance for the visitors. If you protect your media assets from hotlinking, you will save your bandwidth from being misused and improve your site performance. Visit Themeum blog for more of such tutorials and tips. And share your ideas with us via comments.