
當我在瀏覽器中輸入網站的非 SSL URL“cms00.example.com”時,它不會重定向到https://cms00.example.com。如果我輸入 HTTP 位址,我可以看到該站點,如果我輸入 HTTPS 位址,我可以看到該站點。我只是無法讓從 http 到 https 的重定向正常工作。我已經閱讀了大量有關如何執行此操作的文章並嘗試了所有建議,但我的配置仍然無法正常工作。我在 Debian 8 上運行 Apache 2.4.10,這是我第一次使用 Apache。
我已運行這兩個命令並驗證重寫和 ssl 模組已加載:
sudo a2emod rewrite # <- already enabled
sudo a2emod ssl # <- already enabled
我沒有對 /etc/apache2/apache2.conf 進行任何修改,也沒有建立任何其他 .htaccess 檔案。
這是我的設定檔:
# /etc/apache2/sites-available/vhosts.conf
DirectoryIndex index.php index.html
<VirtualHost *:80>
ServerName cms00.example.com
DocumentRoot "/var/www/html"
Redirect permanent / https://cms00.example.com
</VirtualHost>
<VirtualHost *:443>
ServerName cms00.example.com
DocumentRoot "/var/www/html"
SSLEngine on
SSLCipherSuite AES256+EECDH:AES256+EDH
SSLProtocol All -SSLv2 -SSLv3
SSLHonorCipherOrder On
SSLCompression off
SSLCertificateFile /etc/apache2/ssl/example.com.crt
SSLCertificateKeyFile /etc/apache2/ssl/private/example.com.key
<Directory "/var/www/html">
AllowOverride All
Options -Indexes +FollowSymLinks
Require all granted
</Directory>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
答案1
我已經解決了這個問題。這是發生的事情。 /etc/apache2/apache2.conf 包含對 /etc/apache2/sites-enabled 中具有符號連結的任何設定檔的呼叫。由於該目錄中有一個符號連結指向 /etc/apache2/sites-available/000-default.conf,因此正在載入後一個設定文件,並且它會覆寫我的 vhosts.conf 檔案中的區塊和指令。一旦我刪除了該符號鏈接,我的 vhosts.conf 設定就能夠生效。我的教訓是,任何在啟用網站中具有符號連結的檔案都將啟用。