Joey Aghion

Half a Mind

by Joey Aghion

Simple 301 redirects

When I switched from to the more personal for this site, I needed a way to permanently redirect all requests for the old hostname to the new one. Github pages (where this is hosted) only supports a single hostname and doesn’t allow for redirects. You could redirect in javascript or with a meta-tag, but I wanted to guarantee that search engines would respect the new location.

Rerouter is a simple rack app that accepts a map of source and destination hostnames. When it receives a request matching a source hostname, it issues a 301 (permanent) redirect to the new hostname while preserving the requested path and querystring.

The complete source is incredibly simple.

require 'rack/rewrite'

# Expects ENV['REDIRECTS'] to be a ruby hash of source => destination hostnames. E.g.:
#   "{'' => ''}"
REDIRECTS = eval(ENV['REDIRECTS'] || '') || {}

use Rack::Rewrite do

  REDIRECTS.each do |from, to|
    r301 %r{.*}, "http://#{to}$&", if: -> (env) { env['SERVER_NAME'] == from }


# Fall back to default app (empty).
run -> (env) { [200, {}, []] }

Steps for setting up your own rerouter on Heroku:

git clone
cd rerouter
gem install heroku
heroku apps:create
git push heroku master
heroku config:set REDIRECTS="{''=>''}"
heroku domains:add

Then, update the DNS of to be a CNAME pointing to the new heroku app.