thisishardtoread

code, thoughts, and learnings

github.com/arbylee

Blacklisting websites with /etc/hosts and apache mod_rewrite

20 Jan 2014

I’ve noticed over the past few months that my Reddit consumption has continued to grow, shrinking the time I spend on side projects and other activities. So, I’ve finally decided to cut Reddit out completely.

By editing the hosts file, you can effectively ban a website from your computer:

# /etc/hosts
127.0.0.1 www.reddit.com

With this change, any request sent to Reddit will now be directed to my local machine (127.0.0.1). This is all you need to prevent access to Reddit, but as an additional measure I decided to set up an apache rewrite rule since I usually have a webserver running:

# /etc/apache2/httpd.conf
# ...
# Make sure this line is not commented out
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
# ...
RewriteEngine On
RewriteCond %{HTTP_HOST} reddit.com [NC]
RewriteRule .\* https://www.codeschool.com [L,R]

Restart apache for the new rule to take effect:

sudo apachectl restart

Now when I try to access Reddit, my machine scolds me: “No, no, no. I think you meant to do something productive. Here’s CodeSchool instead.” Usually that’s enough of a kick to get me working on something else. But sometimes, I just use my phone.