--- title: Referrer-Policy slug: Web/HTTP/Headers/Referrer-Policy tags: - HTTP - 响应 - 请求头 - 请求首部 - 隐私 - 首部 translation_of: Web/HTTP/Headers/Referrer-Policy ---
Referrer-Policy
首部用来监管哪些访问来源信息——会在 {{HTTPHeader("Referer")}} 中发送——应该被包含在生成的请求当中。Header type | {{Glossary("Response header")}} |
---|---|
{{Glossary("Forbidden header name")}} | no |
注意 {{HTTPHeader("Referer")}} 实际上是单词 "referrer" 的错误拼写。Referrer-Policy
这个首部并没有延续这个错误拼写。
Referrer-Policy: no-referrer Referrer-Policy: no-referrer-when-downgrade Referrer-Policy: origin Referrer-Policy: origin-when-cross-origin Referrer-Policy: same-origin Referrer-Policy: strict-origin Referrer-Policy: strict-origin-when-cross-origin Referrer-Policy: unsafe-url
https://example.com/page.html
会将 https://example.com/ 作为引用地址。
你也可以在 HTML 内设置 referrer 策略。例如,你可以用一个 name 为 referrer 的 {{HTMLElement("meta")}} 元素为整个文档设置 referrer 策略。
<meta name="referrer" content="origin">
或者用 {{HTMLElement("a")}}、{{HTMLElement("area")}}、{{HTMLElement("img")}}、{{HTMLElement("iframe")}}、{{HTMLElement("script")}} 或者 {{HTMLElement("link")}} 元素上的 referrerpolicy
属性为其设置独立的请求策略。
<a href="http://example.com" referrerpolicy="origin">
另外也可以在 {{HTMLElement("a")}}、{{HTMLElement("area")}} 或者 {{HTMLElement("link")}} 元素上将 rel
属性设置为 noreferrer
。
<a href="http://example.com" rel="noreferrer">
CSS 可以从样式表获取引用的资源,这些资源也可以遵从 referrer 策略:
no-referrer-when-downgrade
),除非 CSS 样式表的响应消息通过 Referrer-Policy
首部覆盖该策略。style
属性,则遵从文档的 referrer 策略。Policy | Document | Navigation to | Referrer |
---|---|---|---|
no-referrer |
https://example.com/page.html | any domain or path | no referrer |
no-referrer-when-downgrade |
https://example.com/page.html | https://example.com/otherpage.html | https://example.com/page.html |
no-referrer-when-downgrade |
https://example.com/page.html | https://mozilla.org | https://example.com/page.html |
no-referrer-when-downgrade |
https://example.com/page.html | http://example.org | no referrer |
origin |
https://example.com/page.html | any domain or path | https://example.com/ |
origin-when-cross-origin |
https://example.com/page.html | https://example.com/otherpage.html | https://example.com/page.html |
origin-when-cross-origin |
https://example.com/page.html | https://mozilla.org | https://example.com/ |
origin-when-cross-origin |
https://example.com/page.html | http://example.com/page.html | https://example.com/ |
same-origin |
https://example.com/page.html | https://example.com/otherpage.html | https://example.com/page.html |
same-origin |
https://example.com/page.html | https://mozilla.org | no referrer |
strict-origin |
https://example.com/page.html | https://mozilla.org | https://example.com/ |
strict-origin |
https://example.com/page.html | http://example.org | no referrer |
strict-origin |
http://example.com/page.html | any domain or path | http://example.com/ |
strict-origin-when-cross-origin |
https://example.com/page.html | https://example.com/otherpage.html | https://example.com/page.html |
strict-origin-when-cross-origin |
https://example.com/page.html | https://mozilla.org | https://example.com/ |
strict-origin-when-cross-origin |
https://example.com/page.html | http://example.org | no referrer |
unsafe-url |
https://example.com/page.html | any domain or path | https://example.com/page.html |
如果你要为那些策略未获广泛的浏览器支持的情况指定一种后备策略,使用逗号分隔的列表,并将希望使用的策略放在最后:
Referrer-Policy: no-referrer, strict-origin-when-cross-origin
在上面的场景中,no-referrer
仅在 strict-origin-when-cross-origin
不被浏览器支持的情况下被使用。
规范 | 状态 |
---|---|
Referrer Policy | 草稿 |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
{{Compat("http.headers.Referrer-Policy")}}
注意: 从版本 53 起,Gecko 在 about:config 中提供了一项偏好设置,使得用户可以自行设定默认的 Referrer-Policy 值 —— network.http.referer.userControlPolicy 。可选的值包括:
no-referrer
same-origin
strict-origin-when-cross-origin
no-referrer-when-downgrade
(the default)referrer
.referrerpolicy
attribute on an {{HTMLElement("a")}}, {{HTMLElement("area")}}, {{HTMLElement("img")}}, {{HTMLElement("iframe")}}, or {{HTMLElement("link")}} element.noreferrer
link relation on an a, area, or link element (rel="noreferrer"
).