.htaccess - 将 HTTP 流量从 443 端口重定向到 SSL

标签 .htaccess redirect ssl https virtualhost

我有一个站点 www.example.com,它使用 RewriteEngine 将 HTTP 流量重定向到 HTTPS:

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName example.com
    ServerAlias www.example.com

    DocumentRoot /var/www/example.com/
    ....

    RewriteEngine on
    RewriteCond %{HTTP_HOST}   ^example.com [NC]
    RewriteRule   ^(.*)$ https://www.example.com$1  [L]
    RewriteCond %{SERVER_PORT} ^80$
    RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
</VirtualHost>

我发现的问题是谷歌已经将这个 URL 编入索引:

http://www.example.com:443

给出这个错误:

enter image description here

我尝试重定向流量,添加来自 80 和 443 虚拟主机的永久重定向:

<VirtualHost *:80>
    ...
    RewriteEngine on
    #RewriteCond %{HTTP_HOST}   ^example.com [NC]
    #RewriteRule   ^(.*)$ https://www.example.com$1  [L]
    #RewriteCond %{SERVER_PORT} ^80$
    RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
</VirtualHost>

<VirtualHost *:443>
    ...
    RewriteEngine on
    RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
</VirtualHost>

没有成功。重定向来自 http://www.example.com:443 的所有流量的正确方法是什么? (和派生页面)到 https://www.example.com

最佳答案

What's the right way to redirect all traffic from http://www.example.com:443 (and derived pages) to https://www.example.com?

没有这样的方法:

  • http://www.example.com:443 表示建立TCP连接到443端口,然后直接讲HTTP
  • https://www.example.com:443 表示建立到端口 443 的 TCP 连接,进行 TLS 握手,然后在此 TLS 连接内使用 HTTP。

如您所见,两种访问都使用到同一端口的 TCP 连接,但一个访问服务器使用 HTTP,而另一个访问同一服务器的 TLS(相同的主机和端口意味着相同的服务器)。虽然理论上可以区分 HTTP 请求(对于 http://...:443)和 TLS 握手的开始(对于 https://.. .:443) 并单独处理它们这样的功能通常不受服务器支持,因为通常的方式是使用端口 80 用于 http,端口 443 用于 https。

这意味着,服务器期望在端口 443 进行 TLS 握手。服务器检测到它没有获得 TLS 握手,而是收到 HTTP 请求,并向客户端返回“错误请求”消息,说明这是对该端口的请求类型错误。

关于.htaccess - 将 HTTP 流量从 443 端口重定向到 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30456257/

相关文章:

javascript - 在 .JS 文件中使用 PHP?

regex - 重定向到其他域但保留键入的域

javascript - 当前目录的 404 重定向工作

c - 从 C 重新路由 stdin 和 stdout

node.js - 重定向时 Express 上未设置 Csrf token

ssl - 错误 : SSL_connect returned=1 errno=0 state=SSLv3 read server hello A: sslv3 alert handshake failure

.htaccess - 如何将域名转发到现有 URL - .htaccess、DNS 记录,还有什么?

Javascript - 页面重新加载脚本不起作用

wso2 api 管理器中的 SSL 证书配置

asp.net - 如何在 http 和 https 之间共享 asp.net session