Prevent Hotlinking with htaccess

By | 2011/05/27


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 thought on “Prevent Hotlinking with htaccess

Leave a Reply

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

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