Jeremy Smith on February 14, 2017

Managing Redirects in Middleman

If you need to maintain redirects for old pages in a Middleman site, you can do so by adding something like this to your config file:

redirect "/my/old-page.html", to: "/my/new-page/"

If you’ve only got a few redirects, this isn’t too bad. But if you have dozens or hundreds, all those redirect calls makes reading your config much more difficult. Instead, try this.

Create a data file for your redirects in data/redirects.yml with the original path and the desired redirect.

 - original: training/old-registration
   redirect: /training/registration
 - original: training/old-course
   redirect: /training/course
 - original: software/project1
   redirect: https://github.com/company/project1
 - original: software/project2
   redirect: https://github.com/company/project2
 - original: training/additional
   redirect: /training

Then, in your config.rb, you can simply iterate over your redirects from the data file.

data.redirects.each do |redirect|
  redirect "#{redirect.original}/index.html", to: redirect.redirect
end

Notes

The Redirects documentation shows a leading slash on the original path. However, at the time of this writing, there seems to be an issue with that leading slash on the original path.

Also, I’m using Directory Indexes, so my example config uses "#{redirect.original}/index.html", because it’s matching paths ending with /index.html.


Need help building or maintaining a Rails app?

Book a one-hour project inquiry call or reach out via email.