Redirect www to Non-www Domain at Heroku
Last updated
Table of Contents
- Redirect www to non-www over HTTP
- Redirect www to non-www over HTTPS
- Redirect HTTP to HTTPS
- Have more questions?
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
-
Add a URL record with set to
wwwand the destination set tohttp://example.com. - 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
-
Add a URL record with set to
wwwand the destination set tohttps://example.com. - 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. -
Check that the redirect works for both
http://www.example.comandhttps://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
-
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 -
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 aswhispering-willow-5678.herokudns.com - A CNAME record with set to
wwwand set to the DNS target forwww.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.
- An ALIAS record with an empty and set to the DNS target for
- Get a certificate for both host names at Heroku. Either run
heroku certs:auto:enableto use Heroku’s Automated Certificate Management, or install a certificate from DNSimple that covers both names. - 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
- Make sure the host name points to its
herokudns.comDNS target and has a certificate at Heroku, as in Redirect in your Heroku app. - 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-Protoheader 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.