apache - 如何从 https 子域 URL 中删除 www

标签 apache .htaccess ssl mod-rewrite elastic-load-balancer

我有一个通过弹性负载均衡器 (ELB) 在​​ AWS 上设置的域。为了启用 SSL,我已将 AWS 托管安全证书 (*.domain.us) 添加到负载均衡器。我需要能够安全地访问以下 8 个 URL 组合:

[1] http://www.domain.us
[2] http://www.subdomain.domain.us
[3] http://domain.us
[4] http://subdomain.domain.us
[5] https://www.domain.us
[6] https://www.subdomain.domain.us
[7] https://domain.us
[8] https://subdomain.domain.us

凭借通配符 AWS 证书,1、4、5 和 8 自动为我工作。但是对于剩余的 URL 类型,我已经更新了我的 .htaccess 文件,如下所示: (如果我只是访问 [7] 而没有将其转换为 www.domain.us,我会收到“连接不安全”错误,因为我的 AWS 证书是通配符证书并且不能简单地在 https://domain 上工作.us。但是 https://www.domain.us 或 https://subdomain.domain.us 可以正常工作,因为有通配符证书)

RewriteEngine On

# domain.us => www.domain.us -------------------------------------------
RewriteCond %{HTTP_HOST} ^domain.us
RewriteRule ^ http://www.domain.us%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^domain.us
RewriteRule ^ https://www.domain.us%{REQUEST_URI} [R=301,L]

# www.subdomain.domain.us => subdomain.domain.us -----------------------
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.domain\.us)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]

# NO WWW   http://www. becomes always http://
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\.(.+\.domain\.us)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# http => https ------------------------------------------------------------
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

<Files 403.shtml>
order allow,deny
allow from all
</Files>

我在ELB上监听80和443端口,并将每个端口对应的数据发送到实例端口。我在 EC2 实例上打开了 80 和 443 端口。

我已经浏览了很多 SO 帖子并创建了上面的 .htaccess 文件,但我仍然无法拒绝。 [6] 和 [7] 有效。我仍然在浏览器中收到“连接不安全”错误。有人可以帮忙吗?

最佳答案

这是一个很常见的“问题”,或者更确切地说是按设计工作。您在浏览器中明确调用 HTTPS,因此在交换任何数据之前建立 HTTPS 连接。但是当你的证书没有涵盖的时候,它注定会失败

[6] https://www.subdomain.domain.us
[7] https://domain.us

您必须在处理 SSL-/TLS-Offloading 的组件中实现一个逻辑,以便首先更改为 http 或正确的 HTTP_HOST BEFORE 建立 SSL 连接。

所以你要走的路是:

不要使用显式 HTTPS 调用 URL! :)

让服务器执行此操作。您的服务器配置可以缩短为:

RewriteCond %{HTTP_HOST} !^.+\.domain.us
RewriteRule ^ https://www.domain.us%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.(.+\.domain\.us)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,QSA,L]

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

请注意,%{REQUEST_URI} 包括 %{QUERY_STRING}。所以查询字符串总是在你重写时被丢弃。

关于apache - 如何从 https 子域 URL 中删除 www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43183851/

相关文章:

apache - .htaccess 重定向到目录中的文件

apache - Htaccess 重定向 - 将许多规则合并到一个重定向中

.htaccess 或其他区分大小写的 URL

wcf - 具有相互 SSL 的自托管 WCF 服务(在服务和客户端之间)因 403 Forbidden 而失败

apache - 如何使用 Apache 反向代理动态端口?

php - 检测到路径错误的 Apache 服务

java - SSL套接字服务器在握手后获取证书cn

wordpress - 如何修复 Chrome 中的 https "not secure"消息?

apache - 使用代理服务器的 HTTP 到 HTTPS 映射

regex - (Apache)正则表达式匹配特定的 cookie 名称+值(剥离/删除它)?