formats

Prevent Hotlinking with htaccess

Published on May 27, 2011, by + in linux, sysadmin.







Hotlinking is the use of another person’s hosted image embedded on a different site without permission. I’ll show you how to prevent hotlinking with htaccess!




Place the following in an .htaccess file on your web server to prevent hotlinking of images but however allow google, bing, and yahoo image searches access.

Change the first entry from this example to your own domain:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?yourdomain\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?google\.(.+)/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?(.*\.)?google\.(.+)/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?bing\.(.+)/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?(.*\.)?bing\.(.+)/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?yahoo\.(.+)/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?(.*\.)?yahoo\.(.+)/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [F]

This just gives a 403 forbidden and will not allow the image to be hotlinked.

You can test hotlinking on this page or this page.

Sweet!

One Response

  1. Be

    Thanks! Works like a charm. :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.

Home linux Prevent Hotlinking with htaccess