Stop Image Hotlinking using .htaccess

In general images from millions of web sites gets crawled by Google. Using great images on your site also increases your chances to get noticed by Google searchers. Even though this could be a great way to generate few extra traffic on your site but could really become a pain in the neck if people starts hotlinking your images / graphics to their site.

What’s Image Hotlinking?

Using image resources directly from one web site(site-A) to a another(site-B). In case of hotlinking “site-B” does not host the image yet uses URL of “site-A” to show content on the site. Allow me to give you an example.

Let’s assume, I am have an image of a star which located at the following URL.

https://iftekhar.net/images/star.jpg

Now, some random dude from online finds this image as Google crawls my images, and decides to use my image on his site using this same URL. Every time someone sees this image on his site, is actually using my bandwidth for that image to show up. That’s image hotlinking. Enough intro, let’s get to the point.

Luckily on Apache server you can easily stop this using simple snippet on your .htaccess file.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond "%{HTTP_REFERER}" "!^$"
RewriteCond "%{HTTP_REFERER}" "!www.mydomain.com" [NC]
RewriteRule "\.(gif|jpg|png)$" "-" [F,NC]
</ifModule>

Assuming that your domain name is “www.mydomain.com”. The snippet mentioned above will deny any request from any domain name for image file types (gif,jpg, png) except your very own domain(www.mydomain.com) name. So you should be able to add images on your site safely.

Note: Using this snippet will prohibit Google from crawling your images as well. If you still want Google to keep crawling your images on their server, you can use the following line within your code and it should be ok.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond "%{HTTP_REFERER}" "!^$"
RewriteCond "%{HTTP_REFERER}" "!www.mydomain.com" [NC,OR]
RewriteCond "%{HTTP_REFERER}" "!www.google.com" [NC]
RewriteRule "\.(gif|jpg|png)$" "-" [F,NC]
</ifModule>

Hope this post was helpful for some of you.

Resource: mod_rewrite

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.