I'm trying to write the "ultimate" anti hot linking .htaccess...
You can find many examples/tutorials/generators on the net but many of them are wrong or incomplete (or even both).
These are the features I'm looking for:
- Must block hot linking for a list of file extensions when HTTP_REFERER is an foreign site.
- Must allow hot linking for the current domain (duh) without harcoding it in the .htaccess.
- For the current domain it must work under http and https.
- For the current domain it must work with www and without www.
- Must be able to add exceptions domains to these rules (like our friend Google) and these domains must work under http and https and with www or without www.
This is what I've achieved so far:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?mydomain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com/.*$ [NC]
RewriteRule \.(gif|jpe?g|png|zipx?)$ - [NC,F]
</IfModule>
My questions:
- How to avoid to hardcode
mydomain.com
in the .htaccess? (It would be great to be able to deploy this .htaccess to all my domains without having to modify it for each of them.) - In my RewriteRule,
gif|jpe?g|png|zipx?
is equivalent togif|jpg|jpeg|png|zip|zipx
right? (Sorry still new at regular expressions.) - Do you see anything bad in my .htaccess that I'm unaware of?
For #1 I know it's somewhat possible. The closest I found is this snippet that removes the www from the URL without hardcoding the domain. Is there a way to use this method to my question #1?
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteCond %{HTTPS}s/%1 ^(on(s)|offs)/(.+)
RewriteRule ^ http%2://%3%{REQUEST_URI} [L,R=301]
Update:
I'm aware of solutions that will serve a watermarked image instead of the regular one. But I'm not looking for this kind of solution. I want a universal solution (serve 403 errors) that will work for all kind of binary files (zip, exe, iso, jpg, png, gif...).