【问题标题】:Apache SSL Rewrite with Wildcard Subdomains带有通配符子域的 Apache SSL 重写
【发布时间】:2014-02-28 21:38:03
【问题描述】:

我正在尝试使用 RewriteEngine 在 Apache 中设置 SSL 重定向,该重定向将执行以下操作:

我的理由是我正在开发一个在启动之前使用免费 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]

【问题讨论】:

    标签: apache mod-rewrite ssl


    【解决方案1】:

    想通了。通过将这些规则从站点可用/默认文件移动到网站根目录内的 .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 配置中会更高效。
    猜你喜欢
    • 2012-08-09
    • 2019-11-12
    • 2014-05-10
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 2019-01-07
    • 2012-08-20
    • 1970-01-01
    相关资源
    最近更新 更多