Disable Hotlinking

If other websites are hotlinking to your documents or images, you can save your server resouces and bandwidth by disabling hotlinking.



RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com/.*$ [NC]
RewriteRule .*\.(gif|jpg|png|ico)$ - [F,L]

In the above example, you would replace “example.com” with your domain name. You would also replace the file types specified on the “RewriteRule” line with the file types you wish to disable hotlinking to. Each file type is seperated by a “|”.

Instead of just displaying an error page, you can also specify what would be served should someone try to hotlink to your site:



RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com/.*$ [NC]
RewriteRule .*\.(gif|jpg)$ http://www.example.com/nohotlink.gif [R,NC,L]

In the above example, anyone who tries to hotlink to your site will be served with the image “nohotlink.gif”.

You can also allow hotlinking to only certain sites that you trust.



RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?friend\.com/.*$ [NC]
RewriteRule .*\.(gif|jpg)$ http://www.example.com/nohotlink.gif [R,NC,L]

The above example would allow your domain and the domain “friend.com” to hotlink from your site. Anyone else would be served with the image “nohotlink.gif”.