無法從 http 重定向到 https

無法從 http 重定向到 https

我確信這個問題已經被回答了一百次以上(因為我幾乎已經閱讀了所有這些),但我無法讓我的 apache2 重定向工作。

我對 Linux 上的一切都是一個完全的初學者,我目前正在嘗試運行一個網站。該網站已經可以在 https 下使用並擁有證書。

在連接埠 80 上的錯誤請求

/etc/apache2/ports.conf

Listen 80
<IfModule ssl_module>
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

等/apache2/sites-available/mydomainname.conf

NameVirtualHost *:80
NameVirtualHost <mydomainname>:443
<VirtualHost *:80>
        ServerName <mydomainname>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        RewriteEngine On
        RewriteCond %{HTTPS} off
        RewriteRule (.*) https://%{HTTPS_HOST}%{REQUEST_URI} [R,L]

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost _default_:443>
        ServerName <mydomainname>
        DocumentRoot /var/www/html
        SSLEngine On
</VirtualHost>

sudo a2ensite <mydomainname>.conf如果這很重要的話我也已經執行了。

答案1

您在 :443 中缺少一些重要指令VirtualHost。未指定SSL憑證檔案和私鑰。

VirtualHost指令應如下所示:

Listen 443
<VirtualHost *:443>
    ServerName example.com
    SSLEngine on
    SSLCertificateFile "/path/to/www.example.com.cert"
    SSLCertificateKeyFile "/path/to/www.example.com.key"
</VirtualHost>

查一下官方阿帕奇文檔更多細節:

相關內容