Search code examples
.htaccesshttp-refererreferer

Blocking Visitors Referred From Indicated Domains


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>

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