apache - 我是否缺少将 .htaccess 重定向到 https 的内容?
问题描述
我正在尝试将所有流量从 http 定向到 https:
www.example.com -> https://www.example.com
示例.com -> https://example.com
该服务器是在 Ubuntu 16.04 上运行 LAMP 的 AWS EC2 实例(使用了设置教程)。
SSL 证书由 Let's Encrypt 为 example.com 和 www.example.com 生成(使用教程)。
我已经阅读了十几个 SO 答案和其他教程,但没有一个 .htaccess 示例代码对我有用。这是我尝试过的一些列表,但没有成功(我尝试通过 SFTP 上传文件并通过 nano 进行 SSH 编辑):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]
,
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
,
RewriteEngine On
# https/http www -> https non-www
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# http non-www -> https non-www
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
,
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
,
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
,
RewriteEngine On
RewriteBase /
https://www.primesoft.in
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.%{SERVER_NAME}/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://www.%{SERVER_NAME}/$1 [R=301,L]
任何帮助将不胜感激。
解决方案
经过更多研究,事实证明 Apache 默认关闭了 .htaccess。这个回答解决了它: .htaccess not being loaded in Ubuntu 14.04 (Apache 2.4.7)
推荐阅读
- reactjs - React - onclick 功能在页面加载时运行
- c# - 从 Microsoft C# 生成包含多个工作表的电子表格
- html - 如何将 css 转换为以下样式的样式组件
- python-3.x - TKinter读取csv文件以表格格式显示所有数据为每一行添加按钮
- excel - 使用户表单看起来好像在 Excel 工作表中而不是单独的
- java - Java 8 流,过滤掉 Array 并获取计数,然后遍历数组直到前一个 Count 变量
- javascript - Puppeteer 返回空对象
- python-3.x - KEYCODE_BACK 按钮映射使用哪个 HID 键码?
- c - 将数字扫描到C中的函数中
- r - 从嵌套列表中提取具有特定名称的所有元素