Heroku Redirects with rack-rewrite

A month ago, I moved this blog over to Heroku, and I’ve been really happy. It’s made writing and deploying a lot easier. But I needed a way to redirect the feeds to FeedPress and display my nice 404 page, which I had no idea how to do.

I did some digging and found out I could use Rack::Rewrite. Understanding how exactly to get this to work took me longer than I would’ve liked, but I finally figured it out.


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

# Gemfile
source ""
gem 'rack-rewrite'

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

require 'rack/rewrite'
use Rack::Rewrite do
  r302 '/atom.xml', '', :if => { |rack_env|
    rack_env['HTTP_USER_AGENT'] != 'FeedPress'
  r302 '/atom.articles.xml', '', :if => { |rack_env|
    rack_env['HTTP_USER_AGENT'] != 'FeedPress'

The code above redirects the atom feed to the FeedPress feed unless the User Agent is FeedPress.1 It’s important to note, that you want to put your redirects at the top of your file.2

Now, for the 404 Page

For this, we’ll use a Rack middleware called Rack::NotFound. First we need to add the rack-contrib gem to our Gemfile. If you followed the steps from my move to Heroku, you already have this gem installed.

# Gemfile
source ""
gem 'rack-contrib'

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

require 'rack/contrib/not_found'

What took me a few weeks to figure out, is actually quite simple to do. You can check out the Rack::Rewrite project page for documentation. The rack-contrib documentation is kind of non-existent, but it’s also on GitHub if you want to read through it. Hope you find this helpful!

  1. FeedPress still needs access to the original feed, or else it becomes a never ending loop of redirects and the feed would be blank. 
  2. I honestly don’t know if this makes a difference, but I had them lower in the file and they weren’t working so… I don’t know. 

By Timmmmy

A human hoping to be empathetic and kind with all. I'm a geek, coffee drinker, stoner, maker of things, and forever an optimist. Brightly-colored clothes weren't enough, so I dye my hair too (he/him)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s