Allow/deny image hotlinking with .htaccess
Asked Answered
G

2

12

So I've got this in my site .htaccess file to prevent hotlinking of images, JS and CSS from all other domains.

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com [NC]
RewriteRule \.(gif|jpe?g|js|css)$ - [F,NC,L]

Question: How would I selectively allow one or two domains to hotlink?

Guarantee answered 7/8, 2009 at 16:59 Comment(0)
F
24
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?otherdomain\.com [NC]
RewriteRule \.(gif|jpe?g|js|css)$ - [F,NC,L]

Will work, as this says.

"Refererr is not nothing, and referer is not matching mydomain and referer is not matching otherdomain.

If it were the case that you were trying to do the opposite (blacklist a set of domains from hotlinking) you'd do something like

RewriteCond %{HTTP_REFERER} ^http://(www\.)?baddomain1\.com [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?baddomain2\.com [NC]
RewriteRule \.(gif|jpe?g|js|css)$ - [F,NC,L]
Frankforter answered 7/8, 2009 at 17:10 Comment(3)
Jeez, that was easy. Should have realized that myself! Thanks!Guarantee
How to define the hostname dynamically?Aguilar
is this work for sub domain also? like nv.example.com?Wayward
R
3

Just add another condition before the RewriteRule for each domain you want to allow.

RewriteCond %{HTTP_REFERER} !friendlysite\.com [NC]  

(presumably you don't care if the request is via http or https or whatever, so you can leave that out to make it more generic)

Rothschild answered 7/8, 2009 at 17:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.