GitHub 页面 https/www 重定向

GitHub Pages https/www Redirect

当使用 GitHub 页面托管静态网站时,如何获得 https://www.test.com to redirect to https://test.com

我最近通过在我的 DNS 提供商 (namecheap) 设置 A 记录为我的静态站点启用了 TLS(由 GitHub/Lets Encrypt 提供)。我还在我的 GitHub 存储库设置中选择了 "Enforce HTTPS" 选项,它处理来自 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 的重定向请求。

关于这个问题,GitHub 说,"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."

...我的 DNS 提供商说,"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)."

我似乎陷入了这两个服务的无限循环,说另一个应该提供此功能。

对我有用的:

  • 将您的自定义域更改为前缀为 www.,如下所示:

  • 保存设置并等待 www.example.com 解决并工作。

  • 去掉www.前缀重新保存

  • 等待浏览器和 DNS 缓存失效。

  • 所有组合都应导致 https://example.com/,并且不应出现 SSL 错误。

GitHub Pages 会自动处理顶级域和 www 子域之间的重定向,只要您正确配置 DNS 记录。

来自 configuring an apex domain 上的 GitHub 页面文档:

If you configure the correct records for each domain type through your DNS provider, GitHub Pages will automatically create redirects between the domains. For example, if you configure www.example.com as the custom domain for your site, and you have GitHub Pages DNS records set up for the apex and www domains, then example.com will redirect to www.example.com. Note that automatic redirects only apply to the www subdomain.

虽然文档讨论了从顶级域重定向到 www 子域,但反向也适用。

如果顶点域是所需的目标(如本问题所示):将自定义域值设置为 example.com.

如果 www 子域是所需的目标:将自定义域值设置为 www.example.com

同样,只要为顶级域和 www 子域正确配置了 DNS 记录,这就有效,如 the docs 中所述。

不是这个问题的重点,但问题 When should one use a 'www' subdomain? 提供了一些关于是否使用 www 的很好的见解。