Cobwwweb logo

Rails on Heroku - Redirect Root Domain to www

Jun 05, 2020 Heroku, Ruby on Rails

I deployed a Ruby on Rails application to Heroku that was being served via a custom domain, using the www subdomain of a domain that I owned (e.g. www.example.com).

But I quickly realized that the www was not enough. Users are going to type in the web address manually, which meant that I also needed to support the root level domain (e.g. example.com). And, to make the experience cleaner, as a best practice, I knew I should redirect the root-level domain to the www subdomain.

Resolving Root Domain on Heroku

It's not as straightforward as you may assume in getting root domains to resolve to your app on Heroku. That's because Heroku requires that all domains use CNAME-like records, and most DNS providers don't offer the ability to create ALIAS or ANAME records for root-level domains.

I've found there are three options that work with varying degrees of success.

Option 1: Forward root domain to "www"

If your DNS host doesn't provide the ability to add ALIAS or ANAME records to your root domain, they likely enable you to forward requests to that root-level domain to another domain.

So, in a pinch, you could forward your root domain to the www version.

The problem with that approach is that it doesn't support SSL certificates. And Heroku provisions these certificates for free.

In other words, if you forward at the domain level, http://example.com could forward to https://www.example.com, but https://example.com would not resolve.

Option 2: Use a naked domain redirect

Another option would be to use a naked (or root) domain redirecting service like wwwizer. In this case, you would create an A record for the root domain, point it to wwwizer's IP address, and it will resolve to the www subdomain.

I've had trouble getting this to resolve in the past, but it's worth a shot if you are registered with a DNS host that doesn't support CNAME-like records on the root domain.

Option 3: Find a DNS host that supports ALIAS records

The option I prefer is to go with a DNS host that support CNAME-like records on the root domain. There are a handful out there, but two of my favorites are Namecheap and DNSimple.

You can transfer your existing domain to one of these services. That process usually takes about a week. And then you'll be able to register the ALIAS record with your new host.

Redirecting Root to "www" Subdomain in Rails

It's not ideal to perform this redirect within the Rails application, as it's going to take a lot longer than if you can do it at the web server level. However, you don't have access to that level with Heroku, and therefore your options are limited.

I accomplished this by adding a before_action hook in my ApplicationController to redirect non-www requests to the www subdomain. That looked something like this:

class ApplicationController < ActionController::Base

  before_action :redirect_root_domain

  # ...

  private

    def redirect_root_domain
      return unless request.host === 'example.com'
      redirect_to("#{request.protocol}www.example.com#{request.fullpath}", status: 301)
    end

end

This will effectively redirect the root-level domain to the "www" subdomain. You'll just want to change out the value for the domain.

Did you learn something or find this article interesting?

If so, why not

Want to receive exactly one email every month with new articles, tools, and references I've discovered? Sign up below. (More info here.)