Domain works with www, but not without the www

I am hosting a nodejs server with a static site in a docker container on fly.io.
I own a domain that i purchased through hover.com, and i set up the DNS records as such:

However, although https://www.mydomain.com works, http://mydomain.com works, but https://mydomain.com/ doesn’t.

I get:

This site can’t be reached

poshfob.com unexpectedly closed the connection.

Try:

ERR_CONNECTION_CLOSED

@user84 Does the your Fly dashboard give you the green light on your certificate DNS validations? That _acme-challenge entry doesn’t look complete to me — I would have thought you’d need two entries there for _acme_challenge.www.mydomain.com and _acme-challenge.mydomain.com for all your URLs to work correctly.

Sorry i mean to reply to my own… i had a cert for *.mydomain.com, but i also needed one for mydomain.com. Honestly not sure why that fixed it but it did!

1 Like

Although now im struggling how to get http to auto-redirect to https when i am deploying a static site?

Is this still the nodejs app you’re referring to? If it is you’d probably want a middleware that checks the protocol and redirects if it’s http. But this will need to be some kind of behaviour change either way.

I have the opposite of this persons problem. I have a custom domain working at example.com, but I want it to work with the “www” as in "www.example.com". So I want both to work.

Scenario
• Running a Phoenix 1.6 application with Postgres
• I have everything running with very little configuration. So far so good.

Question

  1. Does my Phoenix or Elixir application need to be configured to interpret “www”?

  2. Do I need to add anymore information to my DNS provider so that if someone types “www.example.com” it will be directed to. Right now “example.com” works but I want “www” to work.

  3. When I ping “example.com” I get a response but when I ping it with “www.example.com” it doesn’t work.

  4. Do I need to create 2 SSL certificates? Example A vs Example B

Example A - in this example I have the wildcard in quotes
flyctl certs create example.com
flyctl certs create “*.example.com”

Example B - in this example I have www.example.com but not in quotes
flyctl certs create example.com
flyctl certs create www.example.com

I have run this command so far.
flyctl certs create example.com

The example here says it’ll be like in A for using wildcard, i.e, with quotes.

SOLVED IT
I think I figured out how to get it working. I had to refresh myself on the differences between CNAME vs A Records

STEPS

At this point wait until things have propagated 5 - 15 minutes.

You will know things are working because your certificates are valid and you should be able to visit your custom domain at example.com without the www.

My Understanding
Here is a screenshot of what my final records and record types looks like. (The value column has dummy data but the entries should help anyone understand how the records could be set up.)

Everything highlighted are the records I had to add from the flyio steps mentioned in the steps above (2 A Records, 1 AAA Record, 2 CNAME Records)

In order to get it to work with the www you need to create a CNAME record that points to your root domain. Most of my problem was a result of the CNAME | WWW | record not containing the value of my root domain example.com.

If anything looks incorrect let me know. Hopefully this helps anyone.

Verifying through the terminal

> host www.example.com
  www.example.com is an alias for example.com.
  example.com has address 66.255.125.99

> host example.com
  example.com has address 66.255.125.99
3 Likes

Hi @alenm . Did you have to create a new cert for the www domain?

Yes

@alenm Thanks for your comment. I managed to get my custom domain at GoDaddy to point to my app hosted on fly in no small part due to your comments above. So, thanks gain.

Not sure if you have any idea how to accomplish the following:

So,

  1. http://example.com resolves to https://example.com
  2. http://www.example.com resolves to https://www.example.com
  3. https://example.com stays at https://example.com
  4. https://www.example.com stays at https://www.example.com

#2 and #4 above are ok of course,
but for #1 and #3 I’d like them to resolve to https://www.example.com.

Does your site always resolve to https://www.example.com

For services configured in fly.toml, there’s a force_https parameter that can be set to true. To redirect non-www to www, that’s not something at Fly can do directly.

Your options would be…

  1. Handle it in your application with some sort of middleware.
  2. Put a webserver like Caddy or Nginx in front of your app and configure the redirect there – this can be another Fly app that talks to your main app over the internal network.
  3. Put Cloudflare in front of your Fly app and use Page Rules to enforce the redirect.

Thanks @brian one quick follow-up question. So, the reason I wanted it to resolve to www. was because I noticed that in places such as nytimes.com it does resolve to www. and was wondering if that is necessary for say SEO purposes or if I leave it as is per my previous comment then if it will negatively impact me from an SEO standpoint.

It took me a few tries to get everything working and now I have CI/CD setup and except for the resolving to www. all seems well so if I do not have to fiddle with this anymore then I’d prefer that.

Again, thanks for your time.

@brian I just checked and yes, seems I always had this force_https = true

so, this is what I have

  [[services.ports]]
    handlers = ["http"]
    port = 80
    force_https = true