Apache SSL 通配符子域名重定向

3
我正在尝试使用RewriteEngine在Apache中设置SSL重定向,以执行以下操作:
- 将流量重定向到http://mydomain.comhttp://www.mydomain.com来使用HTTPS。 - 将其他任何子域https://*.mydomain.com的流量重定向为HTTP而不是HTTPS。
我的理由是,我正在开发一个在推出之前使用免费SSL证书的项目。该证书覆盖了基本域,但没有通配符子域,并且每次访问其中一个子域时需要绕过警告,这很烦人。
编辑: 我相信我已经接近成功,但我仍然无法使HTTPS到HTTP的重定向正常工作。
RewriteEngine on
# Redirect domain and www to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} =mydomain.com [or]
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect wildcard subdomains to HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
1个回答

4

我搞定了。将这些规则从sites-available/default文件移动到网站根目录内的.htaccess文件中,我成功地使其正常工作。

RewriteEngine on
# Redirect domain and www to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} =mydomain.com [or]
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect wildcard subdomains to HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !=www.mydomain.com
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

感谢这些规则,帮助我实现了通配符域名特定重定向。你是否在Apache虚拟主机配置的SSL部分中添加了HTTPS->HTTP重定向?这可能是为什么没有在.htaccess中工作的原因。如果可以的话,将其放入Apache配置中会更有效率。 - Brian Hogg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接