将HTTP重定向到HTTPS的问题

时间:2016-08-25 07:31:27

标签: apache apache2 vhosts apache2.4

我在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/barhttps://example.comfoo/bar而不是https://example.com/foo/bar/

2 个答案:

答案 0 :(得分:1)

尝试以下方法:

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

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

另外,如果这个问题与Apache 2.4有关,那么你的allow语法应该是Require all granted 在你应该替换:

Order allow,deny
Allow from all

有关详细信息,请参阅https://httpd.apache.org/docs/2.4/upgrading.html

答案 1 :(得分:0)

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

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