差异处理virtualhost http和https

时间:2016-04-07 20:02:25

标签: http ssl https virtualhost

我有这个VirtualHost文件:

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

<VirtualHost *:443>
    DocumentRoot /var/www/dir
    ServerName www.example.com
    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/example.crt
    SSLCertificateKeyFile /etc/ssl/private/example.key
    <Directory /var/www/dir>
        AllowOverride All
    </Directory>
</VirtualHost>

当我浏览以下示例链接时,我得到了这个结果:

1. URL http://www.example.com loads fine.
2. URL http://example.com loads incorrect.
3. URL https://www.example.com loads fine.
4. URL https://example.com loads fine.

现在,我知道如何纠正子弹 2 。我的问题是:为什么子弹2需要像这样的单独的VirtualHost,

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

但在SSL下它加载正常。

1 个答案:

答案 0 :(得分:0)

更改

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

<VirtualHost *:443>
DocumentRoot /var/www/dir
ServerName www.example.com
SSLEngine on
SSLCertificateFile /etc/ssl/certs/example.crt
SSLCertificateKeyFile /etc/ssl/private/example.key
<Directory /var/www/dir>
    AllowOverride All
</Directory>
  </VirtualHost>

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

<VirtualHost *:443>
DocumentRoot /var/www/dir
ServerName example.com
ServerAlias www.example.com
SSLEngine on
SSLCertificateFile /etc/ssl/certs/example.crt
SSLCertificateKeyFile /etc/ssl/private/example.key
<Directory /var/www/dir>
    AllowOverride All
</Directory>
 </VirtualHost>