Redirecting www to Non-www Domain at Heroku
Table of Contents
- Redirecting www to not-www (without HTTPS)
- Redirecting www to not-www (with HTTPS)
- Redirect HTTP to HTTPS
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 aren’t familiar with this topic, you can take a look at how redirects work with HTTPS first.
Redirecting 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
-
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
tohttp://example.com
:- Add
www
as Name - Add
http://example.com
as URL
-
And 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
-
Add both host names to the Heroku application:
$ heroku domains:add example.com
$ heroku domains:add www.example.com
-
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 endpointawesome-application.herokuapp.com
- Add a CNAME record to point
www.example.com
to Heroku. The Name of the record iswww
and the Content field is the application endpointawesome-application.herokuapp.com
- Add an ALIAS record to point
-
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.
Redirecting 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.
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
-
Add both host names to the Heroku application:
$ heroku domains:add example.com
$ heroku domains:add www.example.com
-
Order the SSL certificate and add it to Heroku(https://devcenter.heroku.com/articles/ssl):
$ heroku certs:add server.crt server.key
-
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 endpointexample.com.herokudns.com
- Add a CNAME record to point
www.example.com
to Heroku. The Name of the record iswww
and the Content field is the SSL endpointwww.example.com.herokudns.com
- Add an ALIAS record to point
-
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)
-
Add both host names to the Heroku application:
$ heroku domains:add example.com
$ heroku domains:add www.example.com
-
Enable the Heroku SSL endpoint. Heroku will provide you a specific SSL endpoint similar to
encrypted-application.herokussl.com
. -
Order the SSL certificate and install it on Heroku.
-
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 endpointencrypted-application.herokussl.com
- Add a CNAME record to point
www.example.com
to Heroku. The Name of the record iswww
and the Content field is the SSL endpointencrypted-application.herokussl.com
- Add an ALIAS record to point
-
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
-
Add the host names to the Heroku application:
$ heroku domains:add example.com
-
Order the SSL certificate and install it on Heroku. Heroku will provide you a specific SSL endpoint similar to
example.com.herokudns.com
. -
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 pointexample.com
to Heroku. Leave the Name of the record empty and set the Content field to the SSL endpointexample.com.herokudns.com
- If the host name is a subdomain (e.g.
www.example.com
): Add a CNAME record to pointwww.example.com
to Heroku. The Name of the record iswww
and the Content field is the SSL endpointexample.com.herokudns.com
- If the host name is the root domain (e.g.
-
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)
-
Add the host names to the Heroku application:
$ heroku domains:add example.com
-
Order the SSL certificate and install it on Heroku. Heroku will provide you a specific SSL endpoint similar to
encrypted-application.herokussl.com
. -
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 pointexample.com
to Heroku. Leave the Name of the record empty and set the Content field to the SSL endpointencrypted-application.herokussl.com
- If the host name is a subdomain (e.g.
www.example.com
): Add a CNAME record to pointwww.example.com
to Heroku. The Name of the record iswww
and the Content field is the SSL endpointencrypted-application.herokussl.com
- If the host name is the root domain (e.g.
-
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.