【问题标题】:Issues redirecting HTTP to HTTPS将 HTTP 重定向到 HTTPS 的问题
【发布时间】:2017-01-01 12:40:50
【问题描述】:

我在 Apache 2.4.7 上有一个带有 https 的网站

我想要: http://example.com 转发到https://example.com

请注意,此 URL 没有尾部斜杠!

和

http://example.com/whatever 转发到https://example.com/whatever/

请注意,此 URL 的尾部有斜杠!

目前我有以下:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias *.example.com
    Redirect / https://example.com
</VirtualHost>

<VirtualHost *:443>
    ServerName example.com
    ServerAlias *.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/example.com/

    SSLEngine on
    SSLCertificateKeyFile /etc/ssl/ssl.key/example.com.key
    SSLCertificateFile /etc/ssl/ssl.crt/example.com.crt
    SSLCertificateChainFile /etc/ssl/ssl.crt/example.com.ca-bundle

    <Directory /var/www/example.com/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example-error.log
    CustomLog ${APACHE_LOG_DIR}/example-access.log combined

</VirtualHost>

以上将http://example.com正确转发到https://example.com。

可惜上面也转发了:

http://example.com/foo/bar 到 https://example.comfoo/bar 而不是 https://example.com/foo/bar/

【问题讨论】:

  • 你说你想将http://example.com/whatever重定向到https://example.com/whatever/,后来你说http://example.com/foo/bar 应该重定向到https://example.com/foo/bar。如果原始请求中不存在斜杠,您是否要添加斜杠?
  • 好收获。固定!

标签: apache apache2 vhosts apache2.4


【解决方案1】:

尝试以下方法:

Redirect / https://example.com
RedirectMatch ^(/.+)$ https://example.com$1/

我认为当前的重定向只会匹配主页,所以不知道你为什么会被重定向到 /foo/bar

另外,如果这个问题真的是关于 Apache 2.4,你的允许语法应该是 Require all granted 而不是 如您应该替换:

Order allow,deny
Allow from all

请参阅https://httpd.apache.org/docs/2.4/upgrading.html 了解更多信息。

【讨论】:

  • 我应该用“要求全部授予”替换什么?为什么?
  • 这行得通,但这会将http://example.com/whatever 转发到https://example.com/whatever,而不是https://example.com/whatever/(即带有斜杠。我只是不想要主页的斜杠。
【解决方案2】:

使用重写引擎。我有这个片段:

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

【讨论】:

  • 这会将http://example.com 转发到https://example.com/ 而不是https://example.com(即没有尾部斜杠)。另外我认为不需要检查 HTTPS 条件,否则它会通过端口 443 连接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-08
  • 2018-03-30
  • 1970-01-01
  • 1970-01-01
  • 2017-08-13
  • 2013-11-02
  • 1970-01-01
相关资源
最近更新 更多