I’ve setup my system to block access to a list of websites, and have implemented a simple HTTP server in ruby to redirect traffic to this page.

There is a xkcd comic about distractions on the internet. Its alt-text is inspiring, as is the related blog entry.
It’s always nice to see that we are not alone. Oh, I can focus on a task for hours and ignore everything else, but there are times when I end up with 20 open browser tabs and can’t help but feel that I’m wasting my time.

Some websites are way more useless than others, and I decided that I wanted some sort of warning any time I was about to end up there (a link somewhere else, deliberate choice).

My personal solution consists in a series of hosts file entries plus a tiny local webserver.

Blocking the websites

The first step is to reroute to localhost all requests to the blacklisted websites, so I’ve added these entries to my /etc/hosts/ file.

# previous entries (...)   website_1.com   website_2.com   website_3.com

This setup means that browsers (or any other clients) on the computer trying to connect to one of those hosts will instead be sent to the local machine on port 80, the default port for HTTP.
This will effectively block access to those websites, and anything available on localhost:80 will be displayed instead. Normally you shouldn’t have any server running there, so browsers will just show a plain error message.

An alternative would be to use the IP address of some other site you want to land on. In my case I wanted to land on a simple static page I created, which can’t be reached via IP address without some hacking server side and a lot of pain (I don’t have an IP address to use for this).

The Redirection

The second step is to somehow handle the requests landing on localhost.
As I mentioned, I’ve setup a landing page for this purpose, and I’ve written a tiny ruby HTTP server that redirects there all incoming requests. It can be manually launched as a daemon or it can be executed on system startup.

Note that a local file would work as well, as it can just be served by the server instead of redirecting the requests.