You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

EC2 Apache对接CloudFront时.htaccess重定向失效问题

Apache + CloudFront环境下HTTPS/WWW重定向规则失效问题

我在EC2上部署了Apache虚拟服务器,直接对接CloudFront分发。使用以下.htaccess规则实现HTTP转HTTPS、WWW转非WWW重定向,但规则未生效:

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配置截图:
CloudFront配置 + Route 53配置

内容的提问来源于stack exchange,提问作者bgbs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 15:27:36