如何使用 amazon lightsail + s3 重定向 + 路由 53 强制 HTTPS

How to force HTTPS with amazon lightsail + s3 redirection + route 53

我用 Amazon Lightsail 创建了一个 wordpress 博客,并在 route 53 上购买了几个域名。我的主要域名是 example.com,我还购买了:

我正在使用 s3 存储桶将所有可能的组合(非 www 和 www)重定向到 www.example.com(在我的 route 53 托管区域中是 www.example.com被重定向到我的 lightsail 静态 IP)。

我已经为 www.example.com

安装了 Lets Encrypt SSL 证书

现在我想强制使用 HTTPS,以便所有请求都重定向到 https://www.example.com (usin 301 redirect) which I'm able to do using my s3 buckets but I'm not able to redirect www.example.com to https://www.example.com,因为它与我的静态 IP 地址相关联。

我应该怎么做才能只允许通过 HTTPS 访问我的博客?我在网上搜索了很长时间,发现一些教程说我应该使用 cloudfront 来处理到 HTTPS 的重定向,但它们从来没有真正适用于我的情况。

多亏了这个post,我终于弄明白了。我修改了 httpd-app.conf 文件以添加 SSL 重定向规则:

RewriteEngine On
RewriteBase /

#SSL redirection
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Rewrite index.php
RewriteRule ^index\.php$ - [S=1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

此规则允许使用我在服务器上安装的 SSL 证书(针对域 www.example.com)将所有 HTTP 调用重定向到 HTTPS。