我在EC2上有Apache虚拟服务器,直接连接到cloudfront分发。
http到https和www到非www重定向不适用于以下.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)(.+) [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]
请求http://example.com返回200 OK
响应头:
HTTP/2 200 OK
content-type: text/html
content-length: 53335
date: Tue, 06 Jun 2023 07:28:02 GMT
server: Apache/2.4.52 (Ubuntu) OpenSSL/3.0.2
last-modified: Thu, 08 Dec 2022 10:14:20 GMT
etag: "d057-5ef4e4ce33700"
accept-ranges: bytes
vary: Accept-Encoding
x-cache: Miss from cloudfront
via: 1.1 259df3f3acee8ca070d87aedc7b2.cloudfront.net (CloudFront)
x-amz-cf-pop: MXP63-P1
alt-svc: h3=":443"; ma=86400
x-amz-cf-id: oxvbXrSwIpgwMjr9_5ucSj0x9YF3qRL4ablFSQqlgT==
X-Firefox-Spdy: h2
这里是请求头
GET / HTTP/2
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,/;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
Sec-GPC: 1
Pragma: no-cache
Cache-Control: no-cache
TE: trailers
即Cloudfront配置+ Route 53配置:
1条答案
按热度按时间bkhjykvo1#
您已指示CloudFront仅通过HTTP访问您的源(Origin domain下的Protocol设置),并且您没有将查看器Host头转发到源(CachingDisabled缓存策略)。这意味着您的查看器请求将与您在源上的预期不同:
您可以通过在CloudFront中进行两项更改来完成此操作:
1.将查看器协议策略更改为将HTTP重定向到HTTP
1.添加将www重定向到apex的CloudFront函数[1]
对于第一个选项,您还可以将origin协议设置为Match Viewer,这样您的origin请求将匹配查看器所看到的内容-并且您的htaccess规则将生效。但是,这将比让CloudFront在边缘更靠近您的查看器执行更低的性能,因为重定向请求现在每次都会增加传输到源的延迟。
[1]请看一下本例中的
index.js
,了解实现此功能的结构部分-https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/redirect-based-on-country