KeiruaProd

Redirecting all pages from one domain to another

At work, we replaced recently a self hosted tool with its SaaS counterpart. It greatly reduced the monthly cost as well as the maintenance effort. During this migration, we replaced the domain name by another, so we had to setup a redirection.

It turns out that DNS cannot perform an HTTP 301 redirection (301=moved permanently) because, well, they are not the same protocol. In order to avoid having duplicated content that could be an issue in terms of SEO, we had to setup a web server that would redirect every existing query to the new webserver.

I was using ruby, so I used a ruby gem, rack-rewrite. The Gemfile is minimalistic:

source 'https://rubygems.org'

gem 'rack', '~>1.5.1'
gem 'rack-rewrite', '~> 1.5.0'
bundle install
require 'rack/rewrite'

use Rack::Rewrite do
  r301 %r{.*}, 'https://communaute.inclusion.beta.gouv.fr$&'
end

class EmptyServer
  def call(env)
    # We need to boot rack, but all the redirections happen inside the rack-rewrite middleware above
  end
end

run EmptyServer.new

That’s it.

See a typo ? You can suggest a modification on Github.