34
votes

How can I get https://www.test.com to redirect to https://test.com when using GitHub pages to host a static website?

I recently enabled TLS (provided by GitHub/Lets Encrypt) for my static site by setting A records at my DNS provider (namecheap). I've also chosen to "Enforce HTTPS" option in my GitHub repository's settings, which handles redirecting requests from http://test.com to https://test.com. I have a redirect configured through my DNS provider which forwards http://www.test.com to https://test.com, but the one missing piece of the puzzle is forwarding https://www.test.com to https://test.com.

Regarding this issue, GitHub says, "If your domain has HTTPS enforcement enabled, GitHub Pages' servers will not automatically route redirects. You must configure www subdomain and root domain redirects with your domain registrar."

... and my DNS provider says, "It is not possible to set up a URL redirect in the account for the TCP port forwarding from http://www.domain.tld (uses port 80) to https://www.domain.tld (working via port 443)."

I seem to be caught in an infinite loop of the two services saying the other should provide this functionality.

2
Use @ajzbc's answer, but reobtain your certificate with certbot, making sure you have both test.com and www.test.com as domains. (-d test.com -d www.test.com instead of -d test.com)Eric Reed
Thanks for this suggestion, @HewwoCraziness. If I were to do this, would I be on the hook for updating the cert every 90 days? As of now, GitHub handles that for me.pdoherty926
Looks like you can add a custom domain with HTTPS enabled under GitHub's settings: ayesh.me/GitHub-HTTPS (If this doesn't work, yeah, you can fallback to manually updating and adding the cert every 90 days, or finding a way to automate that.)Eric Reed

2 Answers

28
votes

What worked for me:

  • Change your custom domain to be prefixed with www., like this: form

  • Save the settings and wait until www.example.com resolves and works.

  • Remove the www. prefix and save again.

  • Wait for browser and DNS caches to invalidate.

  • All combinations should lead to https://example.com/ and no SSL error should appear.

4
votes

The simplest way would be to add a CNAME record from www.test.com to test.com. This will definitely work, I've done it a million times for my clients while I worked at a certain domain provider. I'm not sure why they didn't suggest that. Ports do not matter here as it is a domain redirect.