Netlify 允许您强制执行 TLS。建议在您确定所有 URL 都可以使用 https 之前不要“强制 TLS”
在您确定所有 URL 的前面都带有“https://”之前,请不要选中“强制 TLS”选项!
为什么?
一旦您使用 Netlify 强制 TLS,他们将在您的页面响应标头中设置 STS(Strict-Transport-Security)标头。我不会解释而是you can read about it here。
主要需要了解的是:
一旦受支持的浏览器收到此标头,该浏览器将阻止通过 HTTP 向指定域发送任何通信,而是通过 HTTPS 发送所有通信
请注意,Netlify 的设置(使用强制 TLS 复选框)是让访问浏览器在访问日期过去 1 年 内强制执行此操作!因此,如果您在 https 上遇到任何问题,您的网站将会出现一些问题,直到您能够修复它们。
等等!
您仍然希望您的网站在添加证书后始终提供 https 页面,但不要在测试或解决问题时强制使用它。
解决方案:
使用部署站点根目录下的_redirects 文件(在“发布目录,index.html 旁边)将流量重定向到 https。
这是一个文件示例
_redirects
# redirect netlify sitename to your sitename for SEO purposes,
# to avoid duplicate content. Do this for http and https
https://example.netlify.com/* https://www.example.com/:splat 301!
http://example.netlify.com/* http://www.example.com/:splat 301!
# also redirect http to https for your custom domain.
# Note that netlify automatically redirects to your custom domain from the bare domain (or vice versa), so you only need one rule here.
http://www.example.com/* https://www.example.com/:splat 301!
或者使用Structured Redirects进行相同的重定向
netlify.toml
[[redirects]]
from = "https://example.netlify.com/*"
to = "https://www.example.com/:splat"
status = 301
force = true
[[redirects]]
from = "http://example.netlify.com/*"
to = "http://www.example.com/:splat"
status = 301
force = true
[[redirects]]
from = "http://www.example.com/*"
to = "https://www.example.com/:splat"
status = 301
force = true
注意:
- Netlify 已经将裸域重定向到
www 子域(可选)
- 建议使用
www 作为您的自定义域,以充分利用具有 ANY DNS 设置的 Netlify CDN。
- 将 netlify 子域站点重定向到自定义域(可选)
- 将所有路径的
http 重定向到https