Question

I'm trying to block visitors if they are from certain referers via .htaccess (Apache).

Found this code and variants multiple places on the web, but it seems to block all traffic, instead of just the referring domains:

# block visitors referred from indicated domains
<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{HTTP_REFERER} sweetfreestuff.com [NC,OR]
 RewriteCond %{HTTP_REFERER} wormhole.com [NC,OR]
 RewriteRule .* - [F]
</ifModule>

Also have tried this variation, with no change:

# block visitors referred from indicated domains
<IfModule mod_rewrite.c>
 Options +FollowSymlinks
 RewriteEngine on
 RewriteCond %{HTTP_REFERER} sweetfreestuff\.com [NC,OR]
 RewriteCond %{HTTP_REFERER} wormhole\.com [NC,OR]
 RewriteRule .* - [F]
</ifModule>
Was it helpful?

Solution

Found another route to go.. still .htaccess, but a different syntax that separates out the testing of the referrer from the forbidding of the referrer.

# Deny access to all with status "banned"
SetEnvIfNoCase Referer "^http://([a-z0-9\-]+\.)?sweetfreestuff\.com.*$" banned

# Enable Rewrite mode
Options +FollowSymlinks
RewriteEngine On

# 301-Redirect to themselves
RewriteCond %{ENV:banned} ^1$
RewriteCond %{HTTP_REFERER} ^(.*)$

# In any case => 403-Forbidden Page
Order Deny,Allow
Deny from env=banned
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top