Site protest blackouts with .htaccess

Many sites are going to go dark tomorrow (18 January 2012) in protest of the SOPA and PIPA bills currently before US congress. I’m helping Making Light do this, and I thought I’d make a quick note of how we’re going about it.

Google recommends using HTTP 503 “Service Unavailable” status codes. The 503 code indicates that the service (or page, or site) is temporarily unavailable, but that it is expected back again soon. This is better than using the 404 (not found), 302 (
moved temporarily), or 301 (moved permanently) codes, because it tells web crawlers that they should just come back and try again later.

If your site is running on Apache, and is allowed to use mod_rewrite, you can set up a site-wide 503 page with the following steps:

  1. Create an HTML page called 503.html, and upload it in the root of your site. This is just a normal HTML page – no special code needed. Here’s an example: 503_sopa.html
  2. If you don’t already have an .htaccess file for your site, create one (it also goes in the root of your site). If you do already have one, add the following code to the top of the file, but read the notes before you do so:
# =====================
# SOPA Blackout 
# =====================

<IfModule mod_rewrite.c>
# Set a custom error document for 503 errors
ErrorDocument 503 /503_sopa.html

# Cause all requests (except images) to generate a 503 error,
# which will produce the custom 503 error document
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} !=503
RewriteCond %{REMOTE_HOST} !^111\.111\.111\.111$
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteCond %{REQUEST_URI} !robots\.txt$ [NC]
RewriteRule ^ - [L,R=503]
</IfModule>

Important notes on this snippet:

  • The “REMOTE_HOST” line contains an IP address that will be excluded from the blackout rules. Change the IP address listed (111.111.111.111) to your own. This will allow you to keep using your site (and its back-end features, like Movable Type or WordPress), but everyone else will just see the 503 page. If you want to preserve access to your site from multiple IP addresses, you can add multiple REMOTE_HOST lines.
  • The first “REQUEST_URI” line contains exceptions for image files. If you want to serve up an image as part of your 503 file, you need this line, otherwise images will return 503 errors as well. If you don’t need an image, or if you are using images hosted elsewhere, you can remove this line.
  • The second “REQUEST_URI” line ensures that the file robots.txt on your site will be served up normally, without a 503 error.

When you want to restore access to your site, simply remove the code from your .htaccess file, and you’ll be back to normal again.

Update: Matthew Batchelder has a nifty update that uses mod_rewrite date conditions to automatically switch on the 503 redirect rule on 18th January 2011: Preparing a Site for SOPA Blackout with .htaccess