在 Apache 中通过 301 重定向从 HTTP 到 HTTPS 规范化域名时,所有域名都必须在 SSL 证书上吗?

在 Apache 中通过 301 重定向从 HTTP 到 HTTPS 规范化域名时,所有域名都必须在 SSL 证书上吗?

我有几个域名都指向同一个服务器。其中只有一个域名(主域名)实际上在我的 SSL 证书上。httpd.conf一段时间以来,我已将以下内容包含在我的文件中,因此它们都永久重定向到单个域名。

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com$1 [R=301,L]

目的是所有域名都必须 301 重定向到www.example.comHTTPS。然而,我最近注意到这些重定向正在发生 - 但作为 302 重定向。我尝试了健全性检查这里并且它似乎按照我期望的方式工作。

总结一下:

发生的情况:http://sub1.example.com导致 302 重定向到https://www.example.com

应该发生的情况:http://sub1.example.com导致 301 重定向到https://www.example.com

我的问题是,是否所有域名都必须列在 SSL 证书上才能进行 301 重定向?我只是在瞎猜。

只是为了完整性,这里是发生这种情况时的初始页面和目标页面的请求/响应标头。

初始页面的请求标头(sub1.example.com):

GET / HTTP/1.1
Host: sub1.example.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
Upgrade-Insecure-Requests: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8

初始页面的响应头(sub1.example.com):

HTTP/1.1 302 Found
Date: Wed, 04 Oct 2017 21:05:16 GMT
Server: Apache
Location: https://www.example.com/
Cache-Control: max-age=1
Expires: Wed, 04 Oct 2017 21:05:18 GMT
Content-Length: 212
Keep-Alive: timeout=5, max=150
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8

目标页面的请求标头(www.example.com):

GET / HTTP/1.1
Host: www.example.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
Upgrade-Insecure-Requests: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8
Cookie: <redacted>

目标页面的响应标头(www.example.com):

HTTP/1.1 200 OK
Date: Wed, 04 Oct 2017 21:05:16 GMT
Server: Apache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Vary: Accept-Encoding
Content-Encoding: gzip
Keep-Alive: timeout=5, max=150
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

答案1

如果您看到 302 重定向,则似乎“其他内容”最终触发了该重定向。您发布的代码(如果执行)显然是 301。

为了实现 301 重定向,所有域名是否都必须列在 SSL 证书上?

否,只要您只是重定向HTTP——正如您的示例所显示的那样。

如果您的其他域名未在 SSL 证书上列出,那么您在尝试从 重定向时显然会遇到问题https://otherdomain.com/...,因为请求永远不会到达您的服务器(除非用户接受了无效的证书)。

相关内容