PHPackages                             stevie-ray/referrer-spam-blocker - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. stevie-ray/referrer-spam-blocker

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

stevie-ray/referrer-spam-blocker
================================

Apache, Nginx, IIS, uWSGI, Varnish, HAProxy &amp; Lighttpd blacklist plus Google Analytics segment to prevent referrer spam traffic

v1.1.21(2mo ago)3815085MITVCLPHP &gt;=8.3CI passing

Since Jan 21Pushed 2mo ago51 watchersCompare

[ Source](https://github.com/Stevie-Ray/referrer-spam-blocker)[ Packagist](https://packagist.org/packages/stevie-ray/referrer-spam-blocker)[ GitHub Sponsors](https://github.com/Stevie-Ray)[ RSS](/packages/stevie-ray-referrer-spam-blocker/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (10)Dependencies (7)Versions (40)Used By (0)

Referrer Spam Blocker 🤖
=======================

[](#referrer-spam-blocker-robot)

Apache, Nginx, IIS, uWSGI, Caddy, Varnish, HAProxy &amp; Lighttpd blacklist + Google Analytics segments to prevent referrer spam traffic

[![Latest Stable Version](https://camo.githubusercontent.com/edd580798f9fd6170a4ea492b0f901555d368f97661cee65f675fcb734dad431/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7374657669652d7261792f72656665727265722d7370616d2d626c6f636b6572)](https://packagist.org/packages/Stevie-Ray/referrer-spam-blocker)[![Build Status](https://camo.githubusercontent.com/5ed41872714e06d838a8df0822903ab94c6b57d030ecefac9efb64b6972a26a9/68747470733a2f2f7472617669732d63692e6f72672f5374657669652d5261792f72656665727265722d7370616d2d626c6f636b65722e737667)](https://travis-ci.org/Stevie-Ray/referrer-spam-blocker)[![Libraries.io dependency status for latest release](https://camo.githubusercontent.com/c21d00a5872e13750ff19dda3d72a8c5124cb693d6379a031ef60f90f1b19839/68747470733a2f2f696d672e736869656c64732e696f2f6c6962726172696573696f2f72656c656173652f6769746875622f7374657669652d7261792f72656665727265722d7370616d2d626c6f636b6572)](https://libraries.io/github/Stevie-Ray/referrer-spam-blocker)[![Code Quality](https://camo.githubusercontent.com/28bcfe0460b2d79fa8fdcf480de71590f8ef6993eada5ee17d7f21d97641ff89/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f5374657669652d5261792f72656665727265722d7370616d2d626c6f636b6572)](https://scrutinizer-ci.com/g/Stevie-Ray/referrer-spam-blocker/?branch=master)[![Packagist](https://camo.githubusercontent.com/35d7982fa7883240fcef71ac36e49a8632e93c61f89cfc07bf7515d5b60dc7c1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f5374657669652d5261792f72656665727265722d7370616d2d626c6f636b6572)](https://packagist.org/packages/stevie-ray/referrer-spam-blocker/stats)[![License](https://camo.githubusercontent.com/63d9bb8565cadb2e1716cc320e1d9e1e14d5f4e76c70a9fcdae68244532c9143/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7374657669652d7261792f72656665727265722d7370616d2d626c6f636b6572)](https://packagist.org/packages/Stevie-Ray/referrer-spam-blocker)

---

Apache: .htaccess
-----------------

[](#apache-htaccess)

.htaccess is a configuration file for use on web servers running Apache. This file is usually found in the root “public\_html” folder of your website. The .htaccess file uses two modules to prevent referral spam, mod\_rewrite and mod\_setenvif. Decide which method is most suitable with your Apache server configuration. This file is **Apache 2.4** ready, where mod\_authz\_host got deprecated.

Nginx: referral-spam.conf
-------------------------

[](#nginx-referral-spamconf)

**IMPORTANT:** You must increase the map hash bucket size to support the large domain list. With `referral-spam.conf` in `/etc/nginx`, include it globally from within `/etc/nginx/nginx.conf`:

```
http {
	map_hash_bucket_size 128;
	include referral-spam.conf;
}

```

Add the following to each `/etc/nginx/site-available/your-site.conf` that needs protection:

```
server {
	if ($bad_referer) {
		return 444;
	}
}

```

**Performance Note:** This configuration uses a performance-optimized approach with hostname matching instead of thousands of regex patterns. Only one regex is evaluated per request to extract the domain from the Referer header, significantly improving NGINX performance compared to traditional regex-based blocking methods.

Varnish: .refferal-spam.vcl
---------------------------

[](#varnish-refferal-spamvcl)

Add `referral-spam.vcl` to **Varnish 4** default file: `default.vcl` by adding the following code right underneath your default backend definitions

```
include "referral-spam.vcl";
sub vcl_recv { call block_referral_spam; }

```

IIS (Internet Information Services): web.config
-----------------------------------------------

[](#iis-internet-information-services-webconfig)

The web.config file is located in the root directory of your Windows Server web application.

Caddy (HTTP/2 Web Server with Automatic HTTPS): referral-spam.caddy and referral-spam.caddy2
--------------------------------------------------------------------------------------------

[](#caddy-http2-web-server-with-automatic-https-referral-spamcaddy-and-referral-spamcaddy2)

Move this file next to your Caddy config file, and include it by doing:

```
# For Caddy 1:
 include ./referral-spam.caddy;
# For Caddy 2:
 import ./referral-spam.caddy2

```

Then start your caddy server. All the referrers will now be redirected to a 444 HTTP answer

uWSGI: referral\_spam.res
-------------------------

[](#uwsgi-referral_spamres)

Include the file `referral_spam.res` into your vassal .ini configuration file:

```
ini = referral_spam.res:blacklist_spam

```

HAProxy: referral-spam.haproxy
------------------------------

[](#haproxy-referral-spamhaproxy)

Use it in your HAProxy config by adding all domains.txt items, in any frontend, listen or backend block:

```
acl spam_referer hdr_sub(referer) -i -f /etc/haproxy/referral-spam.haproxy
http-request deny if spam_referer

```

Lighttpd: referral-spam.lighttpd.conf
-------------------------------------

[](#lighttpd-referral-spamlighttpdconf)

Include this file in your main `lighttpd.conf`:

```
include "referral-spam.lighttpd.conf"

```

Make sure `mod_rewrite` is enabled in your `server.modules`:

```
server.modules = ("mod_rewrite", ...)

```

The configuration blocks referrer spam by redirecting requests with spam referrers. For better performance with large domain lists, consider using `mod_magnet`.

OpenLiteSpeed: .htaccess
------------------------

[](#openlitespeed-htaccess)

OpenLiteSpeed is Apache-compatible and supports `.htaccess` files. Simply use the Apache `.htaccess` file (see Apache section above).

Make sure `mod_rewrite` is enabled in your OpenLiteSpeed configuration:

- Admin Panel &gt; Server &gt; Modules &gt; mod\_rewrite (enable)

Options for Google Analytics 'ghost' spam
-----------------------------------------

[](#options-for-google-analytics-ghost-spam)

The above methods don't stop the Google Analytics **ghost** referral spam (because they are hitting Analytics directly and don't touching your website). You should use filters in Analytics to prevent **ghost** referral spam and hide spam form the **past**. Because Google Analytics segments are limited to *30.000* characters the exclude list is separated into multiple parts.

Navigate to your Google Analytics Admin panel and add these Segments:

FilterSession**Include**Hostnamematches regex```your-website.comFilterSession**Exclude**Sourcematches regexCopy all the domains from [google-exclude-1.txt](https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/google-exclude-1.txt) to this fieldDo the same for [google-exclude-2.txt](https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/google-exclude-2.txt). Please note there may be more files in the future.

You can also prevent **ghost** referral spam by:

- [Adding a filter](https://support.google.com/analytics/answer/1033162)
- [Enabeling bot and Spider Filtering](https://plus.google.com/+GoogleAnalytics/posts/2tJ79CkfnZk)

Command Line Interface
----------------------

[](#command-line-interface)

```
# Basic usage
php run.php
php run.php --types apache,nginx
php run.php --dry-run
php run.php --output /path/to/configs

# Options: -h (help), -v (version), --dry-run, -o (output), -t (types)
# Supported types: apache, nginx, varnish, iis, uwsgi, caddy, caddy2, haproxy, lighttpd, google
```

Testing
-------

[](#testing)

The project includes comprehensive testing and code quality tools:

```
# Run tests
composer test
composer test-coverage

# Code quality
composer phpstan
composer phpcs
composer phpcbf
composer quality
```

Tests cover unit testing, configuration generation, domain processing, and file operations. Quality tools include PHPStan (Level 8), PHP CodeSniffer (PSR-12), and Psalm for static analysis.

Programmatic Usage
------------------

[](#programmatic-usage)

```
use StevieRay\Generator;

$generator = new Generator('/path/to/output');
$generator->generateFiles();
$generator->generateSpecificConfigs(['apache', 'nginx']);
$stats = $generator->getStatistics();
```

Intregrate in a Dockerfile
--------------------------

[](#intregrate-in-a-dockerfile)

You can also integrate these configuration file in your Docker repo, so you will get always the most updated version when you build your image. For `Apache, Nginx, Varnish 4` or `IIS` add the following line to your `Dockerfile`

```
# Apache: Download .htaccess to /usr/local/apache2/htdocs/
ADD https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/.htaccess /usr/local/apache2/htdocs/

# Nginx: Download referral-spam.conf to /etc/nginx/
ADD https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/referral-spam.conf /etc/nginx/

# Varnish 4: Download referral-spam.vcl to /etc/varnish/
ADD https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/referral-spam.vcl /etc/varnish/

# IIS: Download web.config to /sitepath/ (change sitepath accordingly)
ADD https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/web.config /sitepath/

# Caddy: Download referral-spam.caddy to /sitepath/ (next to your Caddy config file given through -conf)
ADD https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/referral-spam.caddy /sitepath/

# uWSGI: Download referral_spam.res to /sitepath/ (change sitepath accordingly)
ADD https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/referral_spam.res /sitepath/

# HAProxy: Download referral-spam.haproxy to /etc/haproxy/
ADD https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/referral-spam.haproxy /etc/haproxy/

# Lighttpd: Download referral-spam.lighttpd.conf to /etc/lighttpd/
ADD https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/referral-spam.lighttpd.conf /etc/lighttpd/

# OpenLiteSpeed: Use the Apache .htaccess file (OpenLiteSpeed is Apache-compatible)
ADD https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/.htaccess /sitepath/

```

Like it?
--------

[](#like-it)

- [Buy me a beer](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=4XC7KX75K6636) 🍺

###  Health Score

62

—

FairBetter than 99% of packages

Maintenance86

Actively maintained with recent releases

Popularity31

Limited adoption so far

Community30

Small or concentrated contributor base

Maturity90

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 61.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~78 days

Recently: every ~25 days

Total

39

Last Release

69d ago

PHP version history (4 changes)v1.0.238PHP &gt;=5.6

v1.0.271PHP &gt;=7.2

v1.1.5PHP &gt;=8.1

v1.1.15PHP &gt;=8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/cd36bd2a4e05e8f37179bf41fe6cf34cd956a492b2f84a441405a2c07445ce73?d=identicon)[Stevie-Ray](/maintainers/Stevie-Ray)

---

Top Contributors

[![Stevie-Ray](https://avatars.githubusercontent.com/u/5747715?v=4)](https://github.com/Stevie-Ray "Stevie-Ray (58 commits)")[![jusurb](https://avatars.githubusercontent.com/u/171747?v=4)](https://github.com/jusurb "jusurb (13 commits)")[![thiagotalma](https://avatars.githubusercontent.com/u/612578?v=4)](https://github.com/thiagotalma "thiagotalma (3 commits)")[![andrewgamez](https://avatars.githubusercontent.com/u/9532365?v=4)](https://github.com/andrewgamez "andrewgamez (2 commits)")[![daugsbi](https://avatars.githubusercontent.com/u/5528706?v=4)](https://github.com/daugsbi "daugsbi (2 commits)")[![delirius](https://avatars.githubusercontent.com/u/2039398?v=4)](https://github.com/delirius "delirius (2 commits)")[![peterjurkovic](https://avatars.githubusercontent.com/u/195626?v=4)](https://github.com/peterjurkovic "peterjurkovic (2 commits)")[![simbus82](https://avatars.githubusercontent.com/u/1375730?v=4)](https://github.com/simbus82 "simbus82 (2 commits)")[![whoacowboy](https://avatars.githubusercontent.com/u/8116953?v=4)](https://github.com/whoacowboy "whoacowboy (2 commits)")[![oddtwelve](https://avatars.githubusercontent.com/u/195200?v=4)](https://github.com/oddtwelve "oddtwelve (1 commits)")[![chrisloftus](https://avatars.githubusercontent.com/u/68920?v=4)](https://github.com/chrisloftus "chrisloftus (1 commits)")[![Pierstoval](https://avatars.githubusercontent.com/u/3369266?v=4)](https://github.com/Pierstoval "Pierstoval (1 commits)")[![brendanmullan](https://avatars.githubusercontent.com/u/4569153?v=4)](https://github.com/brendanmullan "brendanmullan (1 commits)")[![skmbr](https://avatars.githubusercontent.com/u/1945312?v=4)](https://github.com/skmbr "skmbr (1 commits)")[![JoshKoberstein](https://avatars.githubusercontent.com/u/1995321?v=4)](https://github.com/JoshKoberstein "JoshKoberstein (1 commits)")[![generitek](https://avatars.githubusercontent.com/u/33958025?v=4)](https://github.com/generitek "generitek (1 commits)")[![mitchellkrogza](https://avatars.githubusercontent.com/u/9961541?v=4)](https://github.com/mitchellkrogza "mitchellkrogza (1 commits)")

---

Tags

apachegoogle-analiticsgoogle-analyticshaproxylighttpdnginxreferrer-spamreferrer-spam-blockerspam-filteringspam-preventionuwsgivarnishspamnginxanalyticsblacklistapachelighttpdvarnishblockerreferrerhaproxyiisuwsgi

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/stevie-ray-referrer-spam-blocker/health.svg)

```
[![Health](https://phpackages.com/badges/stevie-ray-referrer-spam-blocker/health.svg)](https://phpackages.com/packages/stevie-ray-referrer-spam-blocker)
```

###  Alternatives

[spatie/laravel-analytics

A Laravel package to retrieve Google Analytics data.

3.2k5.7M57](/packages/spatie-laravel-analytics)[rubix/ml

A high-level machine learning and deep learning library for the PHP language.

2.2k1.4M28](/packages/rubix-ml)[segmentio/analytics-php

Segment Analytics PHP Library

25621.5M25](/packages/segmentio-analytics-php)[panphp/pan

A simple, lightweight, and privacy-focused product analytics php package.

1.2k94.6k5](/packages/panphp-pan)[zumba/amplitude-php

PHP SDK for Amplitude

409.5M5](/packages/zumba-amplitude-php)[nystudio107/dotenvy

Speed up your production sites by ditching .env for key/value variable pairs as Apache, Nginx, and shell equivalents.

326.9k](/packages/nystudio107-dotenvy)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
