Go Dark with .htaccess

Go Dark with .htaccess

I know this post is late coming, but hey, it’ll be here the next time the Internet gears up for a protest. Here’s what you need to do so your website is automatically configured to go.

Please note: This discussion is for the Apache web server.

First, create your protest page. For purposes of this discussion, we’ll use Zachary Johnson’s blackout page, which I’ve renamed dark.html.

Next, you’ll want to modify your .htaccess file as follows:

# Allow URL rewriting
Options +FollowSymlinks
RewriteEngine On
# Go Dark Logic
RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY} ^20120118$
RewriteCond %{TIME_HOUR} >7
RewriteCond %{TIME_HOUR} <20
RewriteRule !^dark\.html$ /dark\.html [L]

All lines beginning with # are comments and can be ignored.

The Options and RewriteEngine lines ensure that we can rewrite URLs. They’re probably already in your .htaccess file. If not, stick ‘em in at the top.

The first RewriteCond says the RewriteRule (last line) only applies on January 18, 2012.

The second RewriteCond says the RewriteRule (last line) only applies if the time is later than 7:00am (military time: 0700 hours).

The third RewriteCond says the RewriteRule (last line) only applies if the time is before 8:00pm (military time: 2000 hours).

So, our three conditions are this: Only run the RewriteRule command if it’s January 18, 2012 between the hours of 7:00am and 8:00pm. Beautiful.

The last line is our rewrite. It says if the URL does not start and end with dark.html (!^dark.html$), then rewrite the URL as /dark.html. The L specifies that this is the last rule to run.

So, if someone surfs to http://spiritofnine.com, the site will serve http://spiritofnine.com/dark.html. If someone surfs to http://spiritofnine.com/posts/2011/02/01/whatever.html, the site will serve http://spiritofnine.com/dark.html. No matter which page is requested, dark.html will be served. Nice.

And that’s it. Just comment it all out (make the first character of each Rewrite line a #) and forget about it until the next protest. Then you’ll just need to uncomment, change the date, save it, and you’re golden. Or you could just leave it as is. The conditions won’t be met so the rewrite won’t be executed. But by commenting it out, you’ll save some processing power.

Have fun stormin’ the castle!