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
    Chris Coyier’s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

Incredible Demos

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!