Julian
Julian

Reputation: 9140

.htaccess: How do I block referers EXCEPT for specific dirs?

I want to block all accesses to a website from domain1 and domain2 unless they specifically go to / and /media. So far I have:

RewriteCond %{HTTP_REFERER} domain1\.com [NC,OR]
RewriteCond %{HTTP_REFERER} domain2\.com [NC]
RewriteRule .* - [F]

This works perfectly except that it blocks all requests. I want to allow incoming links from these two domains to be able to access / and /media only. So far I haven't been able to figure it out.

Any help is greatly appreciated.

Upvotes: 0

Views: 136

Answers (2)

TerryE
TerryE

Reputation: 10898

How about:

RewriteCond %{HTTP_REFERER} (:/|\.)(domain1|domain2)\.com [NC]
RewriteRule ^([^/]*$|media/)       -                      [skip=1] 

RewriteCond %{HTTP_REFERER} (:/|\.)(domain1|domain2)\.com [NC]
RewriteRule .*                     -                      [F]

No external redirections are needed. I am assuming here that you'll also allow *.domain1.com etc.

Upvotes: 0

summea
summea

Reputation: 7603

What about something like this:

RewriteCond %{HTTP_REFERER} domain1\.com [NC,OR]
RewriteCond %{HTTP_REFERER} domain2\.com [NC]
RewriteCond %{REQUEST_URI} !media [NC,OR]
RewriteCond %{REQUEST_URI} !^$ [NC]
RewriteRule (.*)$ $1 [R,L]
RewriteRule .* - [F]

Checked the syntax on: http://www.lyxx.com/freestuff/002.html

Upvotes: 2

Related Questions