Blocking Spam with .htaccess File.

Once in a while I will get non-stop comment spam. That’s the reason why comments have to be approved before they show up for everyone else. But its annoying cleaning up spam all the time so a few years ago I started blocking the worst offenders using an .htaccess file. Then over time I would clean up the blocks. It has worked surprisingly well.

But last month I got hit real hard for days from a range of China IP addresses. It was so many IPs my old simple block was not going to cut it so I did some research and found out its possible to block a whole range of IP addresses.

Cyberpunk Spam

How to block with .htaccess

Here is an example of the .htaccess code.

## Spam Block ##
# Block entire 116.179.33.2 – 116.179.33.254 range + your two extra IPs
RewriteCond %{REMOTE_ADDR} ^116\.179\.33\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])$ [OR]
RewriteCond %{REMOTE_ADDR} ^102\.129\.223\.92$ [OR]
RewriteCond %{REMOTE_ADDR} ^159\.146\.41\.7$
# Exclude requests already going to blocked.html to prevent a loop
RewriteCond %{REQUEST_URI} !^/blocked.html$ [NC]
# Redirect to blocked.html
RewriteRule ^.*$ https://www.sysjolt.com/blocked.html [R=302,L]

You can see the spam block in question was 116.179.33.X so I used a regular expression in the rewriteCond to cover the range. I ignored 116.179.33.1 and 116.179.33.255 since those can’t be used.

For a single entry you can see how my old code looked as well when I blocked 102.129.223.92 and 159.146.41.7.

Then I send them to a very plain page with just some text. I don’t want to over burden the server by sending them to a full fancy page with images.

Then to test it all out I put in the IP address of my phone. Once I knew it was working right I took the phone IP and watched my traffic.

Immediately I noticed all the traffic from that range stopped. It worked perfectly!

Dead Internet Theory

Spam is getting worse all the time. It feels like real interactions on the internet are becoming a thing of the past as the dead internet theory comes true. But I enjoy my site so I’m not giving up. I also like to self host my stuff but something like cloudflare could be another option if the .htaccess trick stops working. But I have never used them so I can’t give much detail on how that could be setup.

I hope this helps you!

I hope this little block of code helps you! As of today my friend at https://www.spookyappalachia.com/ has deployed this code on his site. He started getting hit by the very same block of china IP addresses as I was. I don’t know what the deal is with that range but when I heard about it that promoted me to write this to help others.

Affiliate links

Thanks for reading the post! Before you go I'm testing something new. You may see AI generated affiliate links below this text based on the post contents. If you see something intresting then thats awesome. If its way off topic then just ignore them haha. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *