我有兴趣将我的整个网站从 http 切换到 https。
我担心的是我有一些使用绝对 http URL 的内容。
我将需要编辑每个页面以将这些 URL 更改为相对 URL,但这可能需要一段时间才能完成。
我想知道是否有一种方法可以通过 Google 跟踪代码管理器使用 Javascript 将本地绝对 URL 重写为 HTTPS 而不是 HTTP?
如果可行,是否可以将其用作永久解决方案?
最佳答案
要考虑的一个解决方案是 Content Security Policy upgrade-insecure-requests
directive .
The
upgrade-insecure-requests
directive instructs user agents to treat all of a site's unsecure URL's (those served over HTTP) as though they have been replaced with secure URL's (those served over HTTPS). This directive is intended for web sites with large numbers of unsecure legacy URL's that need to be rewritten.
这相当于配置您的 Web 服务器,以便您站点上的所有页面都使用此 header 提供服务:
Content-Security-Policy: upgrade-insecure-requests
因此添加该 header 的效果是:对于您网站上使用 https
URL 提供的任何页面,只要浏览器在其中一个页面中看到 http
嵌入式(子)资源的 URL——无论是样式表、脚本、图像、视频或其他任何内容的 URL——浏览器将自动(透明地)尝试从相应的 https
改为 URL。
更多详情可以查看Upgrade Insecure Requests规范。
2018-05-11更新
所有主要浏览器引擎(包括 Edge 17+ 和 Safari 10.3+)现在都支持 upgrade-insecure-requests
指令:
https://caniuse.com/#feat=upgradeinsecurerequests
现在使用它的缺点是,到目前为止它仅在 Firefox ( since Firefox 42 ) 和 Chrome 中受支持。但它也:
- 有一个open Safari/WebKit implementation-tracking/feature bug
是under consideration by Microsoft for implementation in Edge
P.S.,我在 W3C 工作,我们 recently (finally) enabled TLS/https access to all W3C site resources — 而且由于 W3C 有数十万(也许数百万)个带有用于嵌入子资源的 http
URL 的页面,我们能够实现它的部分方式是通过提供 Content-Security -政策:升级不安全请求
整个网站的 header 。
文章Supporting HTTPS and HSTS on w3.org提供有关部署细节的更多信息。
关于javascript - http 到 https - 使浏览器为 http URL 请求相应的 https URL,而不需要编辑所有页面并手动将所有 URL 更改为 https?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34909224/