Sara
Sara

Reputation: 129

how to Allow hotlinking from other subdomain in .htaccess

I want to deny hotlinks for some files from other sites , but allow it to sub-domains of the same domain

I have written this code which is work great , but the problem is that it block my other sub-domains

RewriteCond %{HTTP_REFERER} !^$
RewriteCond expr "! %{HTTP_REFERER} -strmatch '*://%{HTTP_HOST}/*'"  [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ - [F,NC]

the problem is when a visitor visit www.myDomain.com and requests an image in sub.myDomain.com the hotlink is disallowed

please note thea the domain name is a variable (so I have to use something like %{HTTP_HOST})

Upvotes: 2

Views: 542

Answers (1)

Croises
Croises

Reputation: 18671

You can use:

RewriteCond %{HTTP_HOST} ^(?:[^.]+\.)?([^.]+\.[^.]+)$
RewriteCond %1::%{HTTP_REFERER} !^(.*?)::(?:$|.*\1) [NC]
RewriteRule \.(jpg|jpeg|gif|png|bmp)$ - [F,NC,L]

Upvotes: 2

Related Questions