Redirecting Legacy URLs

Many Refinery sites replace old ones that have links to these older pages. This guide will show you how to:

  • Match old urls and redirect to them appropriate new ones
Edit this guide on Github

1 Meet Rack::Rewrite

Rack::Rewrite is Rack middleware you can add to Refinery to intercept and redirect certain URLs.

2 Add Rack::Rewrite to your Refinery Application

Edit your Gemfile and add this line to the bottom:

gem 'rack-rewrite', '~> 1.0.2'

To install this gem now run

$ bundle install

Finally edit config/application.rb and add the require line and middleware.insert_before block into your file.

module Refinery
  class Application < Rails::Application
    require 'rack/rewrite'
    config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
      # example rewrites
      rewrite '/wiki/John_Trupiano', '/john'
      r301 '/wiki/Yair_Flicker', '/yair'
      r302 '/wiki/Greg_Jastrab', '/greg'
      r301 %r{/wiki/(\w+)_\w+}, '/$1'
      r301 %r{/blog/tag/.*},"/blog"

    # rest of your refinery application module

Restart your server and you’re done.

To better understand the DSL of Rack::Rewrite, check out the documentation, or read how the author of the middleware used it in conjuction with Google