Prevent hotlinking using .htaccess

Bandwidth theft or “hotlinking” is direct linking to a web site’s files (images, video, etc.)

Hotlinking (the act of using images from another site than yours) is unfortunely a common practice which can waste lots of your precious bandwidth. This useful snippets will redirect all hotlinked images to a specific image, defined on line 6.

RewriteEngine On
#Replace ?mysite.com/ with your blog url
RewriteCond %{HTTP_REFERER} !^http://(.+.)?mysite.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
#Replace /images/nohotlink.jpg with your "don't hotlink" image url
RewriteRule .*.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]

You May Also Like

Never Miss Any Web Tutorials, Guides, Tips and Free eBooks

Join Our Community Of 50,000+ Web Lovers and get a weekly newsletter in your inbox

 

I hate spam too. Unsubscribe at any time.

4 thoughts on “Prevent hotlinking using .htaccess”

  1. this code will work great, but i have a doubt,,, i have a subdomain,,, do this work for subdomain?? and also where i want to place this htaccess file for subdomain

    Reply
    • Hi Asho..

      below code is used to prevent hotlinking for all domain and subdomain too..

      RewriteEngine On
      #Replace ?mysite.com/ with your blog url
      RewriteCond %{HTTP_REFERER} !^http://(.+.)?mysite.com/ [NC]
      RewriteCond %{HTTP_REFERER} !^http://(.+.)?subdomain.mysite.com/ [NC]
      RewriteCond %{HTTP_REFERER} !^$
      #Replace /images/nohotlink.jpg with your “don’t hotlink” image url
      RewriteRule .*.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]

      The .htaccess file can be placed on any folder on your site. It has recursive effect. This means that if you place the .htaccess file in your web root (the main folder of your web site) the directives and commands you place in the .htaccess file will have effect on all sub-folders.
      If you place a .htaccess file in a sub-folder, its directives will override the ones that you have in your site main folder.

      Thanks

      Reply
  2. this code will work great, but i have a doubt,,, i have a subdomain,,, do this work for subdomain?? and also where i want to place this htaccess file for subdomain

    Reply
    • Hi Asho..

      below code is used to prevent hotlinking for all domain and subdomain too..

      RewriteEngine On
      #Replace ?mysite.com/ with your blog url
      RewriteCond %{HTTP_REFERER} !^http://(.+.)?mysite.com/ [NC]
      RewriteCond %{HTTP_REFERER} !^http://(.+.)?subdomain.mysite.com/ [NC]
      RewriteCond %{HTTP_REFERER} !^$
      #Replace /images/nohotlink.jpg with your “don’t hotlink” image url
      RewriteRule .*.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]

      The .htaccess file can be placed on any folder on your site. It has recursive effect. This means that if you place the .htaccess file in your web root (the main folder of your web site) the directives and commands you place in the .htaccess file will have effect on all sub-folders.
      If you place a .htaccess file in a sub-folder, its directives will override the ones that you have in your site main folder.

      Thanks

      Reply

Leave a Comment