Movatterモバイル変換


[0]ホーム

URL:


  1. 面向开发者的 Web 技术
  2. HTTP
  3. 参考
  4. HTTP 标头
  5. Referrer-Policy

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in EnglishAlways switch to English

Referrer-Policy

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2020年1月⁩.

* Some parts of this feature may have varying levels of support.

Referrer-Policy 首部用来监管哪些访问来源信息——会在Referer 中发送——应该被包含在生成的请求当中。

Header typeResponse header
Forbidden header nameno

语法

注意Referer 实际上是单词 "referrer" 的错误拼写。Referrer-Policy 这个首部并没有延续这个错误拼写。

http
Referrer-Policy: no-referrerReferrer-Policy: no-referrer-when-downgradeReferrer-Policy: originReferrer-Policy: origin-when-cross-originReferrer-Policy: same-originReferrer-Policy: strict-originReferrer-Policy: strict-origin-when-cross-originReferrer-Policy: unsafe-url

指令

no-referrer

整个Referer 首部会被移除。访问来源信息不随着请求一起发送。

no-referrer-when-downgrade

在同等安全级别或安全级别提升的情况下(HTTP→HTTP、HTTP→HTTPS、HTTPS→HTTPS),在Referer 中发送来源、路径和查询字符串。而在目标的安全级别下降的情况下(HTTPS→HTTP、HTTPS→file)则不发送Referer 标头。

origin

仅在Referer 标头中发送来源。例如https://example.com/page.html 文档会将https://example.com/ 作为引用地址。

origin-when-cross-origin

对于同源的请求,会发送完整的 URL 作为引用地址,但是对于非同源请求仅发送文件的源。

same-origin

对于同源的请求会发送引用地址,但是对于非同源请求则不发送引用地址信息。

strict-origin

在同等安全级别的情况下,发送文件的源作为引用地址 (HTTPS->HTTPS),但是在降级的情况下不会发送 (HTTPS->HTTP)。

strict-origin-when-cross-origin(默认值)

对于同源的请求,发送来源、路径以及查询字符串。对于在相同安全级别的情况下(HTTPS→HTTPS)的跨源请求,仅发送来源。在目标的安全级别下降的情况下(HTTPS→HTTP)则不发送Referer 标头。

unsafe-url

无论是同源请求还是非同源请求,都发送完整的 URL(移除参数信息之后)作为引用地址。

备注:这项设置会将受 TLS 安全协议保护的资源的源和路径信息泄露给非安全的源服务器。进行此项设置的时候要慎重考虑。

集成到 HTML

你也可以在 HTML 内设置 referrer 策略。例如,你可以用一个 name 为 referrer 的<meta> 元素为整个文档设置 referrer 策略。

html
<meta name="referrer" content="origin" />

或者用<a><area><img><iframe><script> 或者<link> 元素上的referrerpolicy 属性为其设置独立的请求策略。

html
<a href="http://example.com" referrerpolicy="origin">…</a>

另外也可以在<a><area> 或者<link> 元素上将rel 属性设置为noreferrer

html
<a href="http://example.com" rel="noreferrer">…</a>

集成到 CSS

CSS 可以从样式表获取引用的资源,这些资源也可以遵从 referrer 策略:

  • 外部 CSS 样式表使用默认策略 (no-referrer-when-downgrade),除非 CSS 样式表的响应消息通过Referrer-Policy 首部覆盖该策略。
  • 对于<style> 元素或style 属性,则遵从文档的 referrer 策略。

示例

PolicyDocumentNavigation toReferrer
no-referrerhttps://example.com/page.htmlany domain or pathno referrer
no-referrer-when-downgradehttps://example.com/page.htmlhttps://example.com/otherpage.htmlhttps://example.com/page.html
no-referrer-when-downgradehttps://example.com/page.htmlhttps://mozilla.orghttps://example.com/page.html
no-referrer-when-downgradehttps://example.com/page.htmlhttp://example.orgno referrer
originhttps://example.com/page.htmlany domain or pathhttps://example.com/
origin-when-cross-originhttps://example.com/page.htmlhttps://example.com/otherpage.htmlhttps://example.com/page.html
origin-when-cross-originhttps://example.com/page.htmlhttps://mozilla.orghttps://example.com/
origin-when-cross-originhttps://example.com/page.htmlhttp://example.com/page.htmlhttps://example.com/
same-originhttps://example.com/page.htmlhttps://example.com/otherpage.htmlhttps://example.com/page.html
same-originhttps://example.com/page.htmlhttps://mozilla.orgno referrer
strict-originhttps://example.com/page.htmlhttps://mozilla.orghttps://example.com/
strict-originhttps://example.com/page.htmlhttp://example.orgno referrer
strict-originhttp://example.com/page.htmlany domain or pathhttp://example.com/
strict-origin-when-cross-originhttps://example.com/page.htmlhttps://example.com/otherpage.htmlhttps://example.com/page.html
strict-origin-when-cross-originhttps://example.com/page.htmlhttps://mozilla.orghttps://example.com/
strict-origin-when-cross-originhttps://example.com/page.htmlhttp://example.orgno referrer
unsafe-urlhttps://example.com/page.htmlany domain or pathhttps://example.com/page.html

指定后备策略

如果你要为那些策略未获广泛的浏览器支持的情况指定一种后备策略,使用逗号分隔的列表,并将希望使用的策略放在最后:

http
Referrer-Policy: no-referrer, strict-origin-when-cross-origin

在上面的场景中,no-referrer 仅在strict-origin-when-cross-origin 不被浏览器支持的情况下被使用。

规范

Specification
Referrer Policy
# referrer-policy-header

浏览器兼容性

备注:从版本 53 起,Gecko 在 about:config 中提供了一项偏好设置,使得用户可以自行设定默认的 Referrer-Policy 值——network.http.referer.userControlPolicy。可选的值包括:

  • 0 —no-referrer
  • 1 —same-origin
  • 2 —strict-origin-when-cross-origin
  • 3 —no-referrer-when-downgrade (the default)

参见

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp