Rewrite rule for blocking hot-link

I would appreciate help in clearing up a rewrite rule for blocking hot-linking.

This works –

RewriteCond %{HTTP_REFERER} !^http://(www\\.)?mysite\\.(net|com)/.*$ [NC]
RewriteRule .*\\.(jpg|txt|css|js)$ / [R,NC,L]

– to redirect any external direct calls to my pictures, text files, style sheets, or scripts to my home page.

With this rule, my own pages can access all those files, except for one – my favicon, which is here:


My .css and .js files are in that same /res/ directory, and are called from the same headers that call the favicon, and they all work with this rewrite rule. Only the favicon is blocked.

Can someone spot the error and show me how to allow the favicon to be accessed by my own pages?

Thank you.

I have the same question as you, I have found my host only allows a selection of htaccess rules, which is why I think mine didn’t work.

The code I used was pretty much the same as yours. I would send an email to your host provider asking if these rules are blocked.



What you need is to FAIL an invalid request …

RewriteCond %{HTTP_REFERER} !^http://(www\\.)?mysite\\.(net|com)/.*$ [NC]
RewriteRule \\.(jpg|txt|css|js)$ - [F]



If the problem is favicon.jpg, try use an GIF, or PNG image for favicon.

Thanks for all these responses.

DK – I tried those lines of code, and still get the same result: all the jpg|txt|css|js files are blocked from direct call, and still show up as expected when called by my own pages – except the favicon, which my own pages can’t access using this code either. Mysterious. I’ve checked and double-checked – same result.

MarPlo – Using a gif for the favicon doesn’t solve the problem when I add gif to the filter (jpg|txt|css|js|gif), though it could conceivably be a kludge, since I have no other gifs in my whole site and thus could allow direct calls to gif (the number of hotlink calls to my favicon likely being, er, small).

Surprise! – It’s a browser problem. I should have thought of this sooner. I mainly use Mac/Firefox.3.6 and that’s where I get the problem. But Safari and iCab display the favicon as expected with either form of the filter (my original and DK’s variant). This obviously isn’t a huge problem but if anyone might suggest why this would happen I’d appreciate knowing, just for the sake of thoroughness. (I could try a new Firefox but prefer to stay for now with 3.6).

I really appreciate the help in this forum.

Or better yet make it an icon (as the name indicates that it is actually supposed to be) - favicon.ico

Bingo! Stephen to the rescue (again)!



Thank you!

This forum is so helpful.