Skip to content
PLAY VIDEO PLAY VIDEO PLAY VIDEO
By Mikel Lindsaar

How to Redirect a Rails Application to a new Domain Name

Rails Applications sometimes have their domain names swapped for something new, this can happen due to a rebranding, sale of the business or any other reason.

In our case it was a rebranding. We had a Ruby on Rails application at https://storeconnect.app and it needed to be moved to https://GetStoreConnect.com.

Looking at the Rails Routing Guide I couldn't see any obvious solution for doing this. I knew I could do it with a redirection Rack application, but I wanted something simple, a one liner even, in the routes.rb file, front and centre and easy to keep track of.

True to form, Rails came through with a simple and maintainable one liner to make it happen. I love working with this language and framework.

This is how I did it:

Rails.application.routes.draw do
  # Permanently redirect all requests originally to storeconnect.app to getstoreconnect.com
  get '/(*path)', to: redirect { |path_params, request|
                          "https://getstoreconnect.com/#{path_params[:path]}"
                        },
                  status: 301,
                  constraints: { domain: 'storeconnect.app' }

  # ... Remaining application routes
end

Simple.

The way it works, is we make a catch all route on get requests to the application. We put the *path matcher in there to capture anything after the forward slash.

We use *path instead of :path so that we get everything, not just the next path segment, otherwise /about/location would not match.

We then also wrap *path in parenthesis to make it optional (otherwise just going to old-domain.com/ would not match).

Then we redirect all of these with a 301 to the new domain and pass the same path variable back in. We can't use the shorthand method of redirect("https://getstoreconnect.com/%{path}") here because the path key may not be present and so would fail on matching the root domain.

We then put a constraint so that this will only match if the request domain is old-domain.com.

And there you go, a one liner (wrapped) :)

Popular Articles by Our Team

Our expert team of designers and developers love what the do and enjoy sharing their knowledge with the world.

We Hire Only the Best

reinteractive is Australia’s largest dedicated Ruby on Rails development company. We don’t cut corners and we know what we are doing.

We are an organisation made up of amazing individuals and we take pride in our team. We are 100% remote work enabling us to choose the best talent no matter which part of the country they live in. reinteractive is dedicated to making it a great place for any developer to work.

Free Community Workshops

We created the Ruby on Rails InstallFest and Ruby on Rails Development Hub to help introduce new people to software development and to help existing developers hone their skills. These workshops provide invaluable mentorship to train developers, addressing key skills shortages in the industry. Software development is a great career choice for all ages and these events help you get started and skilled up.

  • Webinars

    Webinars

    Webinars are our online portal for tips, tricks and lessons learned in everything we do. Make the most of this free resource to help you become a better developer.

    Learn more about webinars

  • Installfest

    Installfest

    The Ruby on Rails Installfest includes a full setup of your development environment and step-by-step instructions on how to build your first app hosted on Heroku. Over 1,800 attendees to date and counting.

    Learn more about Installfest

  • Development Hub

    Development Hub

    The Ruby on Rails Development Hub is a monthly event where you will get the chance to spend time with our team and others in the community to improve and hone your Ruby on Rails skills.

    Learn more about Development Hub

Get the “reinteractive Review” Monthly Email