HTTP到HTTPSredirect失败

当我导航到http://example.com它正确地redirecthttps://example.com

我遇到的问题是,当我导航到http://example.com/sub/directory/page.htm它redirect到https://example.comsub/directory/page.htm

为什么不redirect到https://example.com/sub/directory/page.htm

这里是Apache的configuration:

 <VirtualHost *:80> ServerName example.com Redirect permanent / https://example.com/ </VirtualHost> <IfModule mod_ssl.c> #NameVirtualHost *:443 <VirtualHost *:443> ServerName example.com [...] ProxyRequests Off ProxyPass / http://localhost:8444/ ProxyPassReverse / http://localhost:8444/ </VirtualHost> 

我正在使用的Apache版本:

 Server version: Apache/2.4.18 (Ubuntu) Server built: 2017-09-18T15:09:02 

你的configuration看起来正确:

 Redirect permanent / https://example.com/ 

Redirect指令应该将URL-path逐字符后的所有内容附加到目标。 http://example.com/sub/directory/page.htm的请求确实应该附加sub/directory/page.htmhttps://example.com/并redirect到https://example.com/sub/directory/page.htm

您的评论“当我导航到”似乎表明您正在从网页浏览器进行testing。

这有一些特定的使用现代浏览器的潜在问题:

  • 浏览器caching永久性redirect,因此您在Apacheconfiguration中所做的任何更改都不会被提取,您的浏览器将直接转到caching的目标URL,而无需先连接到Web服务器。
  • 大多数支持TLS并从httpredirect到https的站点也会设置HTTP严格传输安全性标头。 这也应该有一个效果,你的浏览器将不会连接到你的普通http站点,并会主动重写你input到https的普通http URL。
  • 也可能有多个(caching)redirect和/或重写规则,不仅在HTTP站点上,而且在HTTPS站点

有时候,从新的隐身/匿名浏览器窗口进行testing可以防止出现此类问题,但大多数情况下,我只是简单地从命令行进行testing,例如curl -v http://example.com/sub/directory/page.htm