Advanced .htaccess Security – Block Unwanted Referrers

By  on  

For some bloggers and web developers, Digg can be a huge boost in traffic and thus a huge bust in ad revenue. Unfortunately, the Digg Effect can kill a website's bandwidth and get the website shut down. Wouldn't it be great if a weary web developer could prevent his site from being shut down by blocking users referred by Digg, at least a while? Using a small bit of .htaccess code and mod_rewrite, the developer can do just that.

The Code

RewriteEngine on
RewriteCond %{HTTP_REFERER} digg.com [NC]
RewriteRule .* - [F]

Say good-bye to Digg Death with this small, easy-to-place snippet of code!

Recent Features

  • By
    Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

  • By
    Welcome to My New Office

    My first professional web development was at a small print shop where I sat in a windowless cubical all day. I suffered that boxed in environment for almost five years before I was able to find a remote job where I worked from home. The first...

Incredible Demos

  • By
    MooTools Link Fading

    We all know that we can set a different link color (among other properties) on the hover event, but why not show a little bit more dynamism by making the original color fade to the next? Using MooTools 1.2, you can achieve that effect. The MooTools...

  • By
    Reverse Element Order with CSS Flexbox

    CSS is becoming more and more powerful these days, almost to the point where the order of HTML elements output to the page no longer matters from a display standpoint -- CSS lets you do so much that almost any layout, large or small, is possible.  Semantics...

Discussion

  1. David,

    I have a quick question for ya… can you make this .htaccess so you can transfer them to a “slimmed” down version of the same page or simply a blank page that says sorry digg users the site is offline for awhile?

    One more question. When digg or any other site goes down for scheduled maintenance do they use this method to redirect all pages to a sorry we are down page?

    Mark

  2. Not a bad idea with your first question, Mark. Per your idea, I’d create a new sort of page template to do this. Say that “page.php” is getting hit hard and you want to show a slimmed down page. On page.php, you could check the referrer and if the referrer was digg, you could do a header() redirect to “slimpage.php?page=”.(yourpage). You’d them have slimpage.php provide a text-only version of your article.

    As for a site being down, I’d bet that this is what they do. It’s very easy and very simple. Unfortunately, Digg, for example, doesn’t allow you to view their .htaccess file anymore.

  3. What do you think abut blocking : “Options FollowSymLinks”, i think this is important also

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!