Help & Advice Internet Software & Hardware Web Design & Development

Using .htaccess to block unwanted traffic

Not all web traffic is made equal. Some websites and blogs aren’t exactly the kind of places you’d like to get a visit from. So here’s a technique for sending those visitors somewhere else…

Not all web traffic is made equal. Some websites and blogs aren’t exactly the kind of places you’d like to get a visit from. So here’s a technique for sending those visitors somewhere else…

I’ll be honest and say that the Apache web server is mostly beyond my comprehension. However, if you’re serious about web development — and if you are, you use a Unix-flavoured operating system — then you need to know some of Apache’s moves, and probably most important of all, what you can do with the .htaccess file.

Strictly speaking, .htaccess file isn’t a file. As I understand it, it’s the extension of the directory it resides within, which in part explains a little about how the .htaccess works.

First of all, to be able to follow any of the suggestions here, you need:

  • FTP (File Transfer Protocol) access to your website or blog.
  • The right privileges to add or edit a .htaccess file.

If you don’t, then none of the following is going to work for you.

Bouncing unwanted web traffic with RewriteCond

There are certain times when certain types of web traffic is, shall we say, undesirable. I suppose in a way, it’s a mark of the progress I’ve made with the Blah, Blah! Technology blog when I look at some of the undesirable web traffic I receive. So just how did I stop these visits to my blog?

Connect to your website via your FTP client of choice and look for an existing .htaccess file. If there isn’t one, create one and upload it onto the root of your account — the root is the base directory (or folder) that your website resides in.

If there’s already a file in place, make a backup. If the file contains any entries, be sure to ask the relevant parties what they relate to and then add your own entries to the very end of the file.

When you add a .htaccess file there, any instructions added to that file are propagated through all of the directories within the root.

It’s worth mentioning that if you’re on Mac OS X like I am, when creating your .htaccess, beware that the “.” at the beginning of the file name will most likely make the file vanish. That’s because the “.” has special significance within most Unix-based operating systems; instructing the OS to make the file invisible.

So when you create your .htaccess file, create it in the folder where it’s need on your computer; such as the folder within which you’re building your website. If not, you could have a fairly miserable time finding the file again!

Fortunately, most good FTP clients will show these files, or at least offer the option to show them. I use Transmit, by Panic for my FTP needs. I also use, their web development application, Coda, which sort of has Transmit built it.

Next, make sure you’re using a robust text editing application. Again, if you’re on a Mac, you can use either the aforementioned Coda, or BBEdit. I can’t offer any advice on PC equivalents, because I don’t use a PC. But I’m sure you’ll find suitable alternatives.

Within the .htaccess file, add the following:

RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://tools\.pagerush\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://www\.theseomizer\.com
RewriteRule .*[R=301,L]

I’m not going to paw over all of the code in meticulous detail. Instead, I’m going to cover the salient points to help you make this technique work.

The 2nd and 3rd lines are the entries for the websites I’m blocking traffic from. You can add as many websites as you want. When adding the web addresses, follow the formatting in those lines:

  1. place a backslash before each period character;
  2. only use the base web address, unless you want to block a specific web page;
  3. if there’s more than one entry, add the [OR] to end of each line, with the exception of the last.

The final line is the address you’re sending those visitors to. In this case, I’m sending them all to the last page on the internet, which I feel is appropriate.

It’s also worth noting that there’s a benign if not beneficial side to using the RewriteCond. You could, for example, redirect visitors from a website to specific landing page, rather than have them go into the website proper.

Above all, please use this advice diligently and responsibly…

By Wayne Smallman

Wayne is the man behind the Blah, Blah! Technology website, and the creator of the Under Cloud, a digital research assistant for journalists and academics.

7 replies on “Using .htaccess to block unwanted traffic”

Thanks for writing a post that we non-technical bloggers can understand and appreciate. I just got going on my own site, hosted, after years of Typepad et al. It’s daunting, but fun, to explore the possibilities and challenges that others have managed for us bloggers over the years. This is good stuff….

Yes, WP autoformating can be a pain in the neck, there is a fix so that you can embed scripts and php as well as reserved characters in a post. I *think* I described it on but if I didn’t it’s certainly in the Codex.

I’m considering using this technique to bounce StumbleUpon traffic… Sure it’s nice seeing all of those page views – but these people just stumble through… they never leave comments or anything.

Leave a Reply

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