【问题标题】:httpd.conf rewrite https://example.com to https://www.example.comhttpd.conf 将 https://example.com 重写为 https://www.example.com
【发布时间】:2016-04-29 20:32:33
【问题描述】:

在 httpd.conf 中,我在 httpd.conf 中编写了从不安全重定向到安全的规则(例如,http://example.com -> https://www.example.com),

但是当站点已经是 https 时,您如何在 www 前面加上前缀?

例如,如果我在浏览器中输入https://example.com,我找不到让apache 将其重写为https://www.example.com 的方法。相反,我收到证书错误,因为我的证书仅适用于“www.example.com”域。

我目前的试验是:

AllowOverride All
RewriteEngine On
# If www is not present:
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC,OR]
# ... or if https is not present:
RewriteCond %{HTTPS} off
# ensure www and https both present
RewriteRule .? https://www.example.com%{REQUEST_URI} [NC,L]

当我输入“https://example.com”时,上述内容不会在前面加上“www”(希望得到https://www.example.com的结果)

我认为正在发生的事情:我对 https://example.com 的初始请求在执行重定向之前引发了证书不匹配错误。

附带问题:我无法让 google 接受自签名的 *.example.com 证书。如果通配符证书有效,则上述内容不会成为问题。

【问题讨论】:

    标签: apache .htaccess httpd.conf


    【解决方案1】:

    您需要使用 R 标志从非 www 重定向到 www,并在 SSL 上从非 www 重定向到 www 的额外规则。

    AllowOverride All
    RewriteEngine On
    # ...if  https is not present:
    RewriteCond %{HTTPS} off
    # ensure www and https both present
    RewriteRule ^ https://www.example.com%{REQUEST_URI} [NC,L,R]
    #Redirect non-www to www on SSL
     RewriteCond %{HTTPS} on
     RewriteCond %{HTTP_HOST} !^www\.
     RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NC,L,R]
    

    【讨论】:

    • 不幸的是,这不起作用。当我输入example.com 时,我收到一个证书不匹配警告,我想这会阻止处理重定向。但是,从“http://”到“https”+www 的重定向仍然有效。
    猜你喜欢
    • 2014-03-04
    • 2016-10-04
    • 2015-09-25
    • 1970-01-01
    • 2016-01-16
    • 2020-09-24
    • 2011-07-06
    • 2011-08-12
    • 1970-01-01
    相关资源
    最近更新 更多