Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I've been seeing a lot of requests for images hosted on my server from other websites.

Rather than allow this to carry on, I'm considering using mod_rewrite and .htaccess to prevent this using something like

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

I'm sure I read somewhere that there may be some kind of undesirable side-effects or "gotchas" that can catch you out, but can't find the source or anything about it. Most sites only give you the code snippet rather than discussing the advantages and disadvantages of the approach.

Are there any such undesirable side-effects or "gotchas" that I should be aware of if I choose to put this on my server?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

I have been using it in my sites for years (configured using CPanel) and it works fine, as long only want it for saving bandwidth. You only need to remember to update the file when you add another domain/subdomain or they will blocked too.

If properly configured it doesn't block people with blank referrers as suggested in another answer, it only block connections that have a referrer that is not in the allowed list.

Also, I changed the script to redirect to a PHP file, so I can take different actions depending on the domain and the type of file requested:

RewriteRule .*\.(jpg|jpeg|gif|png|mp4|mp3)$ http://www.example.com/hotlink.php?%{SERVER_NAME}%{REQUEST_URI} [R,NC]

In this way the PHP script receive the domain name (I have several domains hosted in the same account) and the URL that triggered the hotlink protection.

share|improve this answer
add comment

The only real gotchas are if you try to use the images from, say, mobile.yourdomain.com or someotherdomainIown.net, and they get blocked by this. I've also seen many admins redirect hotlinked images to other images, like: "This person is stealing bandwidth from yourdomain.com".

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.