Redirecting www to not-www domain at Heroku

Table of Contents


This article contains a list of the most common redirect scenarios for an application hosted on Heroku, along with some solutions for each scenario.

This article contains information about redirects and HTTPS. If you are not familiar about this topic, we encourage you to read how redirects work with HTTPS before moving to the next section.

Redirect www to not-www (without HTTPS)

If your application is using HTTPS, check the section redirect www to not-www with HTTPS.

In this scenario you have an application hosted on Heroku configured with a custom domain (e.g. example.com). You want to redirect the www version (www.example.com) to the same Heroku application (or vice-versa). The application is not using HTTPS.

In this case, the simplest solution is to use our redirector service to redirect http://www.example.com to http://example.com.

To handle the redirect using the redirector
  1. Go to the record editor and add a URL record.

    • Add the redirect subdomain in the Name field.
    • Add the URL of the target domain in the URL field.

    For example, to redirect http://www.example.com to http://example.com:

    • Add www as Name
    • Add http://example.com as URL
  2. That’s it! Check that the redirect works properly.

Alternatively, you can point both the www and not www versions of the site to Heroku, and handle the redirect at the application level.

To handle the redirect at Heroku
  1. Add both host names to the Heroku application:

    • $ heroku domains:add example.com
    • $ heroku domains:add www.example.com
  2. Go to the record editor and add two DNS records, one for each host name, pointing to the Heroku endpoint. Assuming the Heroku endpoint for the application is awesome-application.herokuapp.com:

    • Add an ALIAS record to point example.com to Heroku. Leave the Name of the record empty and set the Content field to the application endpoint awesome-application.herokuapp.com
    • Add a CNAME record to point www.example.com to Heroku. The Name of the record is www and the Content field is the application endpoint awesome-application.herokuapp.com
  3. In your application, intercept the incoming request. If the request host is not the canonical one, redirect the request to the canonical domain. How to perform a redirect depends on the programming language and/or framework your application is developed with.

Redirect www to not-www (with HTTPS)

In this scenario you have an application hosted on Heroku, configured with a custom domain (e.g. example.com). You want to redirect the www version (www.example.com) to the same Heroku application (or vice-versa). The application is also using HTTPS.

In this case, you cannot use our redirector service. The best strategy is to handle the redirect at application level using a certificate that covers both the www and not-www host names.

In order to redirect using HTTPS, you need to have a certificate that covers both the www and not-www (root domain) host names. You can purchase a single-name certificate for www or a wildcard certificates for * to cover the root domain with the same certificate.

To handle the redirect at Heroku
For Heroku SSL
  1. Add both host names to the Heroku application:

    • $ heroku domains:add example.com
    • $ heroku domains:add www.example.com
  2. Order the SSL certificate and add it to Heroku(https://devcenter.heroku.com/articles/ssl):

    • $ heroku certs:add server.crt server.key
  3. Go to the record editor and two DNS records, one for each host name, pointing to the Heroku SSL endpoint:

    • Add an ALIAS record to point example.com to Heroku. Leave the Name of the record empty and set the Content field to the SSL endpoint example.com.herokudns.com
    • Add a CNAME record to point www.example.com to Heroku. The Name of the record is www and the Content field is the SSL endpoint www.example.com.herokudns.com
  4. In your application, intercept the incoming request. If the request host is not the canonical one, redirect the request to the canonical domain. How to perform a redirect depends on the programming language and/or framework your application is developed with.

For Heroku SSL endpoint (legacy)
  1. Add both host names to the Heroku application:

    • $ heroku domains:add example.com
    • $ heroku domains:add www.example.com
  2. Enable the Heroku SSL endpoint. Heroku will provide you a specific SSL endpoint similar to encrypted-application.herokussl.com.

  3. Order the SSL certificate and install it on Heroku.

  4. Go to the record editor and two DNS records, one for each host name, pointing to the Heroku SSL endpoint:

    • Add an ALIAS record to point example.com to Heroku. Leave the Name of the record empty and set the Content field to the SSL endpoint encrypted-application.herokussl.com
    • Add a CNAME record to point www.example.com to Heroku. The Name of the record is www and the Content field is the SSL endpoint encrypted-application.herokussl.com
  5. In your application, intercept the incoming request. If the request host is not the canonical one, redirect the request to the canonical domain. How to perform a redirect depends on the programming language and/or framework your application is developed with.

Redirect HTTP to HTTPS

In this scenario you have an application hosted on Heroku, configured with a custom domain (e.g. example.com). You want to redirect the HTTP version (http://example.com) to the same Heroku application under HTTPS (https://example.com).

In this case, you cannot use our redirector service because it’s not possible to point the same domain at DNS level to two different applications based on the HTTP/HTTPS scheme. The best strategy is to handle the redirect at application level.

To handle the redirect at Heroku
For Heroku SSL
  1. Add the host names to the Heroku application:

    • $ heroku domains:add example.com
  2. Order the SSL certificate and install it on Heroku. Heroku will provide you a specific SSL endpoint similar to example.com.herokudns.com.

  3. Go to the record editor and a DNS records for the host name, pointing to the Heroku SSL endpoint:

    • If the host name is the root domain (e.g. example.com): add an ALIAS record to point example.com to Heroku. Leave the Name of the record empty and set the Content field to the SSL endpoint example.com.herokudns.com
    • If the host name is a subdomain (e.g. www.example.com): Add a CNAME record to point www.example.com to Heroku. The Name of the record is www and the Content field is the SSL endpoint example.com.herokudns.com
  4. In your application, intercept the incoming request. If the request comes from HTTP, redirect the request to the same domain replacing HTTP with HTTPS. How to perform a redirect depends on the programming language and/or framework your application is developed with.

For Heroku SSL endpoint (legacy)
  1. Add the host names to the Heroku application:

    • $ heroku domains:add example.com
  2. Order the SSL certificate and install it on Heroku. Heroku will provide you a specific SSL endpoint similar to encrypted-application.herokussl.com.

  3. Go to the record editor and a DNS records for the host name, pointing to the Heroku SSL endpoint:

    • If the host name is the root domain (e.g. example.com): add an ALIAS record to point example.com to Heroku. Leave the Name of the record empty and set the Content field to the SSL endpoint encrypted-application.herokussl.com
    • If the host name is a subdomain (e.g. www.example.com): Add a CNAME record to point www.example.com to Heroku. The Name of the record is www and the Content field is the SSL endpoint encrypted-application.herokussl.com
  4. In your application, intercept the incoming request. If the request comes from HTTP, redirect the request to the same domain replacing HTTP with HTTPS. How to perform a redirect depends on the programming language and/or framework your application is developed with.