18

I have a domain loading in a web browser using Heroku and Google Domains. Right now the domain loads with a www: http://www. XXX .com.

If I enter the URL w/o the WWW like http:// XXX .com I get the following error in Chrome: "XXX.com’s server DNS address could not be found"

What do I need to do so that the following happens:

  1. This loads - http:// mydomain.com
  2. http://www. mydomain.com redirects to http:// mydomain.com

Thank you

AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012

4 Answers4

22

It's advisable to make the www subdomain the default here, such that the root/naked domain redirects to www. This is in line with Heroku recommendations, which states:

For maximum scalability and resiliency applications should avoid using DNS A-records and instead use a DNS provider that supports CNAME functionality at the apex, or use sub-domains exclusively

You can achieve this with Google Domains Synthetic Records:

  • Go to the Synthetic Records section
  • Select Subdomain forward
  • Enter @ for subdomain, and www.xyz.com for destination
gwcodes
  • 5,632
  • 1
  • 11
  • 20
  • But OP wants the URL in the browser to appear as http:// mydomain.com. Are you saying this isn't possible/recommended? – michaelsnowden Apr 05 '17 at 07:46
  • 7
    @michaelsnowden: for scalability and resiliency, it isn't recommended unless your DNS provider supports CNAME at the apex (or ALIAS or ANAME). You can see that Google Domains doesn't: https://support.google.com/domains/answer/3251147 . Heroku does list the ones which do: https://devcenter.heroku.com/articles/custom-domains#configuring-dns-for-root-domains – gwcodes Apr 05 '17 at 08:01
  • 3
    That's disappointing. I feel like the root domains look a lot better, but I already purchased a google domain. Thanks for the authoritative answer. – michaelsnowden Apr 05 '17 at 08:03
3

Another solution is to use some 3rd party DNS provider like Cloudflare to do this. I just went through this same process. App hosted on Heroku with a domain purchased from Google. Signed up for a free account with Cloudflare and was up and running in about an hour following this guide: https://support.cloudflare.com/hc/en-us/articles/205893698

Things I did:

  • Delete DNS rules I had tried adding earlier to Google under "Custom resource records"
  • Have just one domain added in Heroku for the root: "myapp.com" (not "www.myapp.com", not "*.myapp.com") which gave me a DNS address like "blah-blah-12345-herokudns.com"
  • In Cloudflare add two CNAME records on the DNS page:

    1. CNAME "myapp.com" blah-blah-12345-herokudns.com
    2. CNAME "www" blah-blah-12345-herokudns.com
  • Copy over custom DNS nameservers into Google from Cloudflare

After that now I can go to myapp.com and it just works. Super stoked because I really didn't want www.myapp.com.

One cool thing is I had already set up an GSuite account for the domain and when I signed up for Cloudflare it imported all the DNS rules from Google, including the MX records for email so it worked seamlessly.

KhalilRavanna
  • 5,768
  • 3
  • 28
  • 24
  • Does it take time for this to work? I've followed exactly these steps and it doesn't work :( – weteamsteve Aug 29 '20 at 20:22
  • IIRC it worked almost immediately. I'd either check all these things to make sure they're correct (it's definitely confusing) otherwise maybe they changed how it works since I posted this :/ – KhalilRavanna Sep 06 '20 at 15:51
1

IMHO, you should:

1/ Set your heroku domain to be http://example.com. All the info are in the Heroku documentation.

Heroku should serve your site using http://example.com.

2/ You need to redirect in Google Domains your http://www.example.com to http://example.com. To do this, you need to set web forwarding in the Google Domain console under the website tab. The documentation is straight forward.

Any http://www.example.com request should be forwarded to http://example.com.

UPDATE

Due to your needs, I am wondering if the best is not to disable the naked to www redirection, have both domain points to your heroku instance and get a SSL certificate for them (using let's encrypt makes things easy).

Here is a tutorial.

I hope this helps!

Jeremie
  • 2,241
  • 2
  • 17
  • 25
  • I can't do that using web forwarding because the only options are to "Redirect both example.com and www.example.com to an existing website", and if I enter "https://example.com", then it says "You can't forward a website to itself!". – michaelsnowden Apr 06 '17 at 22:46
  • @michaelsnowden I edited my answer for another method. – Jeremie Apr 06 '17 at 22:56
  • I don't think I can use htaccess on Heroku (http://stackoverflow.com/a/9400862/2770572). Also, example.com doesn't point to my Heroku instance--example.com redirects to www.example.com, which points to my Heroku instance. – michaelsnowden Apr 06 '17 at 23:02
  • I have exactly two settings in Google Domains. 1) A "Synthetic Record" which is a "subdomain forward" from the subdomain "example.com" to "https://www.example.com" via a "temporary redirect (302)" without "path forwarding". 2) A "custom resource record" which is of type "CNAME" with the name "www" and the data "www.example.com.herokudns.com". I need to use "www.example.com.herokudns.com" instead of "example.herokuapp.com" in order for their SSL to work. – michaelsnowden Apr 06 '17 at 23:20
  • So, the www domain works, and that's the expected behaviour. But is the naked domain redirecting to www or to self? You wrote 'the subdomain "example.com" to "example.com";'. – Jeremie Apr 07 '17 at 00:09
  • Unfortunately, the stackoverflow auto-formatting really screwed me up. It should read 1) A "Synthetic Record" which is a "subdomain forward" from the subdomain `example.com` to `https://www.example.com` via a "temporary redirect (302)" without "path forwarding". – michaelsnowden Apr 07 '17 at 01:46
  • I see! I thought so. This sounds good, but you are saying that the naked domain doesn't work? What happen when you try to curl the domain ? Do you get a 302? – Jeremie Apr 07 '17 at 01:49
  • Yeah I get a 302 curling `example.com` which redirects to `https://www.example.com`. – michaelsnowden Apr 07 '17 at 02:37
0

After some research, to set up a redirect from a naked domain (e.g., example.com) to a www subdomain (e.g., www.example.com) using Heroku and Google Domains, you can use a third-party domain host provider like Cloudflare. Cloudflare has a free tier and is easy to use for configuring an HTTP to HTTPS redirect. It is also secure and fast.

Found this link useful on how to do that: https://www.derricksherrill.com/tutorial/web-dev/google-domains-cloudflare/

Pavel Darii
  • 367
  • 3
  • 2