css - HTTPS 以及如何引用文件和图像

标签 css security http https

我应该如何从安全区域的网页引用我的 css 文件(位于非安全区域)。我考虑过复制它(将其中一个移到安全区域),但这似乎效率很低。

非常感谢任何建议。 (p.s. 后面多半会有几个问题哈哈)

最佳答案

您始终可以通过使用相对/根路径来避免此问题:

<link rel="stylesheet" href="/css/screen.css">

如果您必须使用完整的 URL,我不确定为什么您不能使用 https 协议(protocol)(这是正确的解决方案),但还有一个选项:不指定一个协议(protocol)。

<link rel="stylesheet" href="//example.com/css/screen.css">

http://paulirish.com/2010/the-protocol-relative-url/

If the browser is viewing that current page in through HTTPS, then it'll request that asset with the HTTPS protocol, otherwise it'll typically* request it with HTTP. This prevents that awful "This Page Contains Both Secure and Non-Secure Items" error message in IE, keeping all your asset requests within the same protocol.

但是:

Caveat: When used on a <link> or @import for a stylesheet, IE7 and IE8 download the file twice. All other uses, however, are just fine.

因此,如果您必须指定完整的 URL,最好/正确的方法是:

<link rel="stylesheet" href="https://example.com/css/screen.css">

真的别无选择。 CSS 文件本身中图像和资源的相对路径应该适用于任何一种方法,并且不会触发安全错误。如果您需要 CSS 文件中的绝对 URL,则可以使用相同的技巧。

关于css - HTTPS 以及如何引用文件和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8019527/

相关文章:

javascript - 仅当值为 false 时阻止事件并在 Vue 中包含自定义 Bulma 扩展

javascript - 更改 CSS 过渡开始和结束位置

.net - WCF 服务连接问题 - 也许安全?

asp.net - 将 UserId 公开给客户端是否安全?

http - 通常如何发出 HTTP PUT 请求?

html - 我的页面在本地主机上看不到 css 文件

html - 透视元素内的 Firefox 渲染错误

php - 在不处理用户输入时使用未经准备的 SQL 查询的潜在危险?

asp.net - 如何在 AngularJS 2 中传递查询参数?

php - 使用 PHP 和 cURL 发送数据时保留 post 字段的数据类型