Redirect www to Non-www Domain at Heroku

Last updated

Table of Contents


To redirect www.example.com to example.com for a Heroku app, either add a DNSimple URL record for www, so the DNSimple redirector sends visitors to the apex, or point both host names to Heroku and redirect in your app. The same works in the other direction. A redirect from HTTP to HTTPS on the same host name always has to happen in your app.

This article uses example.com as the canonical domain and assumes the apex already points to Heroku, as in Pointing the Domain Apex to Heroku.

Redirect www to non-www over HTTP

If www.example.com only needs to work over HTTP, the redirector is the simplest option. Heroku does not need to know about www.

To redirect with a URL record
  1. Add a URL record with set to www and the destination set to http://example.com.
  2. Check that the redirect works.

Redirect www to non-www over HTTPS

If visitors may open https://www.example.com, the host name that does the redirect needs an SSL certificate. You have two options.

Redirect with the DNSimple redirector

The redirector handles HTTPS redirects when DNSimple has an SSL certificate for the source host name. HTTPS redirects require a Solo Pro plan or higher.

To redirect HTTPS requests with a URL record
  1. Add a URL record with set to www and the destination set to https://example.com.
  2. Get an SSL certificate from DNSimple that covers www.example.com. A free Let’s Encrypt certificate works. See getting a Let’s Encrypt certificate.
  3. Check that the redirect works for both http://www.example.com and https://www.example.com.

Redirect in your Heroku app

Point both host names to Heroku, give Heroku a certificate for both, and redirect in your app.

To redirect in your app
  1. Add both host names to your Heroku app, and note the DNS target Heroku gives for each:

     heroku domains:add example.com -a your-app
     heroku domains:add www.example.com -a your-app
    
  2. In DNSimple, point each host name to its own DNS target:

    • An ALIAS record with an empty and set to the DNS target for example.com, such as whispering-willow-5678.herokudns.com
    • A CNAME record with set to www and set to the DNS target for www.example.com

    The Heroku Connector or the one-click Heroku service can create these records for you. Pointing the Domain Apex to Heroku covers both.

  3. Get a certificate for both host names at Heroku. Either run heroku certs:auto:enable to use Heroku’s Automated Certificate Management, or install a certificate from DNSimple that covers both names.
  4. In your app, check the host of each request. If it is not the canonical host name, redirect to the same path on the canonical host name. How you do this depends on your programming language or framework.

Redirect HTTP to HTTPS

The redirector cannot redirect http://example.com to https://example.com, because both are the same host name and DNS sends them to the same place. Redirect in your app instead.

To redirect HTTP to HTTPS in your app
  1. Make sure the host name points to its herokudns.com DNS target and has a certificate at Heroku, as in Redirect in your Heroku app.
  2. In your app, check the scheme of each request. If it is HTTP, redirect to the same URL with HTTPS. Heroku’s router sets the X-Forwarded-Proto header to the scheme the visitor used.

Have more questions?

If you have any questions about redirecting www to non-www at Heroku or HTTPS redirects, contact support, and we will be happy to help. You can also read more about DNSimple integrations.