A month ago, I moved this blog over to Heroku, and I’ve been re­ally happy. It’s made writ­ing and de­ploy­ing a lot eas­ier. But I needed a way to redi­rect the feeds to FeedPress and dis­play my nice 404 page, which I had no idea how to do.

I did some dig­ging and found out I could use Rack::Rewrite. Understanding how ex­actly to get this to work took me longer than I would’ve liked, but I fi­nally fig­ured it out.

Installation #

First you’ll need to add the Rack::Rewrite gem to your Gemfile:

# Gemfile
source "https://rubygems.org"
gem 'rack-rewrite'

Once you run bundle install it’s time to write our rules:

# config.ru
require 'rack/rewrite'
use Rack::Rewrite do
r302 '/atom.xml', 'http://feedpress.me/theboldreport', :if => Proc.new { |rack_env|
rack_env['HTTP_USER_AGENT'] != 'FeedPress'
r302 '/atom.articles.xml', 'http://feedpress.me/theboldreport-articles', :if => Proc.new { |rack_env|
rack_env['HTTP_USER_AGENT'] != 'FeedPress'

The code above redi­rects the atom feed to the FeedPress feed un­less the User Agent is FeedPress.1 It’s im­por­tant to note, that you want to put your redi­rects at the top of your file.2

Now, for the 404 Page #

For this, we’ll use a Rack mid­dle­ware called Rack::NotFound. First we need to add the rack-contrib gem to our Gemfile. If you fol­lowed the steps from my move to Heroku, you al­ready have this gem in­stalled.

# Gemfile
source "https://rubygems.org"
gem 'rack-contrib'

Run bundle install and we’ll add two more lines to our config.ru file.

# config.ru
require 'rack/contrib/not_found'
run Rack::NotFound.new('/path/to/your/404/index.html')

What took me a few weeks to fig­ure out, is ac­tu­ally quite sim­ple to do. You can check out the Rack::Rewrite pro­ject page for doc­u­men­ta­tion. The rack-contrib doc­u­men­ta­tion is kind of non-ex­is­tent, but it’s also on GitHub if you want to read through it. Hope you find this help­ful!

  1. FeedPress still needs ac­cess to the orig­i­nal feed, or else it be­comes a never end­ing loop of redi­rects and the feed would be blank. ↩︎

  2. I hon­estly don’t know if this makes a dif­fer­ence, but I had them lower in the file and they weren’t work­ing so… I don’t know. ↩︎

Series Jekyll with Heroku

Hacks to get your Jekyll site deployed and thriving on Heroku.

View Series →