header image

How to Set Up Redirects in a Ruby Rack Application

   Back to list

Nowadays, it’s a really popular thing, that a lot of web applications which have been written many years ago in outdated technologies are being migrated to new and shiny frameworks or even completely different technologies.

This is due to a couple of reasons: firstly it’s easier to maintain something that has newer technology than look for developers who are experienced in something really outdated. Secondly, old applications are difficult to extend and maintain.

The main problem with migration is keeping consistency between old routes/links and the new ones. It’s quite important to keep your page in the top search results in Google. You should also remember that you can’t break old links which may still be used.

Let’s say that your webpage was built many years ago just in PHP or ASPX without any modern web framework. It’s likely a lot of your URLs look like this:

  • mywebpage.com/contact.php
  • mywebpage.com/form.php?email=something&process=true
  • mywebpage.com/about.aspx

Now, when you switch to a new framework, for example Rails, it doesn’t use any extensions like .php or .aspx for endpoints, or your URLs are completely different.

Thankfully, there is a really useful Ruby gem called rack-rewrite that can handle all redirects for you. The implementation is really easy and doesn’t take a lot of time. The biggest advantage of this library is the fact that you can use it with pure Ruby application, without Rails.

So, let’s take a look at how you can do this migration and set up redirects in Ruby apps which use Rack.

Installation

For installing, all you need to do is to add this gem to your Gemfile:

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

Then run bundler:

$ bundle install

If you’re using just Rack, add it to config.ru:

If you’re using Rails 4+, you need to add the following code inside your application.rb file:

If you’re using Rails 3 or lower, use this code:

Inside this block, you need to put your redirects (we’ll cover that in the next section). Remember that after every modification in the application.rb file, you should restart your rails server.

Supported redirects

This gem supports redirects with four different HTTP status codes:

  • 301 (moved permanently)
  • 302 (found)
  • 303 (see other)
  • 307 (temporary redirect)
  • 301

If you want to use a redirect with a 301 HTTP status code, all you need to do is to add this line of code:

moved_permanently ‘old_url’, ‘new_url’

This method can be also used as r301 or p (as an alias method).

302

It’s pretty much the same with the 302 HTTP status code, a redirect looks like this:

found ‘old_url’, ‘new_url’

You can also use r302 alias.

303

To see another HTTP status code, the redirect looks like this:

see_other ‘old_url’, ‘new_url’

An alias for this method is r303.

307

For the temporary redirect HTTP status code, it looks like this:

temporary_redirect ‘old_url’, ‘new_url’

This method has two aliases – r307 and t.

Besides these four methods, there is another really useful one called rewrite. The syntax looks like this:

rewrite ‘old_url’, ‘new_url’

The main difference between rewrite and other methods is the fact that the rewrite method rewrites HTTP header values for PATH_INFO and REQUEST_URI to the new url, while a URL in a browser won’t be changed.

You’re not only limited to creating redirects with hardcoded URLs, you can also use regular expressions to catch more URLs, like this:

moved_permanently %r{/wiki/(.*)}, 'http://www.google.com/?q=$1'

Setting big amount of redirects

One thing that probably came to mind was: wow, can I set up a lot of redirects?

Let’s say that you want to add more than 2000 redirects, in this case, you can store them in a file and iterate through it then add all the redirects in each loop inside the application.rb file. For example, you can store all the redirects in a YAML file, like this:

If you want to load this file and add redirects, all you need to do is this:

Another alternative would be to store all the info about the redirect in a YAML file with a status code, old and new URLs, then load it, like this:

Then simply create a file called my_file.yaml:

If you want to find more details about this gem and explore all your options, feel free to visit the gem’s GitHub page. You’ll find an answer to almost everything!

Send this to a friend