307跳转php,http 307重定向

刚才在做hexo页面优化,发现了本地测试返回http 307。以前没见过这个响应码,于是做一下调研。

http_code_307.png

相关文章:

http 307

在rfc规范中,http 307 Temporary Redirect 是临时重定向。

平时常见的重定向是:

301:Permanently Moved,永久重定向

302:Temporarily Moved,临时重定向

http 307和302的区别在于:307要求客户端不改变原先的请求方法,对在Location头部中规定的URI进行访问。对于302,很多客户端的实现是,直接使用GET方式访问重定向地址。

例子

客户端请求

1

2

POST /index.php HTTP/1.1

Host: www.example.org

服务器端响应

1

2

HTTP/1.1 307 Temporary Redirect

Location: https://www.example.org/

那么客户端必须以POST方式重定向访问https://www.example.org/。

本地测试产生http 307

next的_config.yml配置

1

2

3

4

5

6

7

8

9

# Internal version: 2.1.5 & 3.5.7

# See: https://fancyapps.com/fancybox

# Example:

# fancybox: //cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js

# fancybox: //cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.6/jquery.fancybox.min.js

# fancybox_css: //cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css

# fancybox_css: //cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.6/jquery.fancybox.min.css

fancybox: //cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.6/jquery.fancybox.min.js

fancybox_css: //cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.6/jquery.fancybox.min.css

平时写url地址,一般是http或者https,但是next里面的例子都是//。

//的意义是,沿用当前页面的协议。如果当前页面是http协议,那么发出去的请求就是http;如果是当前页面是https,那么发出去走https。

//写法的好处是,不需要关注协议,只需要关注URI路径。如果哪一天发生协议变更,比如http升级为全站https,那么代码完全都不用修改。

但是这没有解释为什么返回了http 307。

http_code_307.png

仔细看看response header,除了Location指示重定向地址外,还有

1

Non-Authoritative-Reason: HSTS

HSTS是HTTP严格传输安全(英语:HTTP Strict Transport Security),之前的文章提到过:

因为本地测试,使用http://localhost:4000访问,所以//的页面协议是http。但是cloudflare.com开启了HSTS,所有请求都必须是https协议。对cloudflare.com原来的http请求必须升级为https。

事实上,这个307响应不是cloudflare.com产生的,是chrome浏览器干的好事。

The way Chrome shows this in the network tab is by creating a dummy 307 response with a redirect to the https version of the address. But that’s a fake response and is not generated by the server - the reality is Chrome did that internally before the request even went to the server.

注意到,rfc定义http 307是Temporary Redirect,而截图显示的是Internal Redirect。 回想到HSTS只在第1次http访问之后才会生效。如果chrome不做这个返回,会是怎样的流程呢:

本地客户端http方式访问cloudflare.com

服务器表示要以https方式访问资源

于是本地客户端以https方式再次访问cloudflare的资源

中途多了一次网络请求。

因为chrome维护了一份HSTS站点列表,知道cloudflare必须要https方式请求。于是截获http请求后,直接以https方式访问,同时做出dummy 307响应。

小实验

把next的_config.yml从//修改为https://,再测试

http_code_200.png

直接就是http 200了。

c711140e3e1385b50a57be3ad89d08bbace.jpg

小结

//比写死具体http、https更加灵活,推荐使用

http 307 Temporary Redirect,临时重定向,客户不能改变请求方式

chrome知道HSTS站点,会自动把这些站点的http请求改写为https,同时在response header增加Non-Authoritative-Reason: HSTS,并且把307响应码解析为Internal redirect

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值