# Redirecting Bad Referers with Varnish

> A Varnish Cache VCL configuration for filtering and redirecting unwanted referral traffic.

- Canonical URL: https://webofmike.com/redirecting-bad-referers-with-varnish/
- Author: Mike Moore (https://webofmike.com/about/)
- Published: 2015-01-16
- Last modified: 2015-01-16
- Tags: Code, Tutorials
- Cite as: Mike Moore, "Redirecting Bad Referers with Varnish", Web of Mike (webofmike.com), 2015-01-16. https://webofmike.com/redirecting-bad-referers-with-varnish/


Spam referrals cluttering analytics is frustrating. Here's a VCL 4.0 approach to redirect suspicious referral sources.

## Implementation

The solution uses a custom subroutine to identify and redirect problematic referrers:

```vcl
sub bad_referrals {
     if ( req.http.referer ~ "hulfingtonpost" ||
          req.http.referer ~ "forum.topic57969834.darodar" ||
          req.http.referer ~ "ilovevitaly" ||
          req.http.referer ~ "priceg" ||
          req.http.referer ~ "blackhatworth.com"
     ) {
          set req.http.host = "www.ihateyousomuchomgroflcats.com";
          return(synth(750, "All your referer are belong to us."));
     }
}
```

The configuration integrates into the receive and synthesis subroutines, ultimately directing flagged traffic via HTTP 301 redirect to an alternative destination.

Administrators can customize the referrer matching patterns and redirect targets according to their specific needs, effectively removing spam referral sources from analytics reporting.

