Apache RewriteRule 未重定向到 www

Apache RewriteRule 未重定向到 www

使用 apache 2.4 並且我的網站已打開www.example.com,它會自動重定向到https://www.example.com,其中它是完全可訪問且功能齊全的。

但是,當我明確嘗試使用http://example.com或者https://example.com,我看到一個佔位符頁面。

我想要發生的是將非 www 重定向到 www。前任 (https://example.com->https://www.example.com

我已在 .conf 檔案中設定了重寫規則,但由於某種原因,它不起作用。造訪非 www 時我看到的只是一個佔位符頁面。

<VirtualHost <ip>:80>
    ServerName www.example.com
    ServerAlias example.com
    # Redirect to www
    RewriteEngine on
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    Redirect permanent / https://www.example.com/
</VirtualHost>

<VirtualHost <ip>:443>
    ServerName www.example.com
    ServerAlias example.com
    SSLCertificateFile /home/admin/certs/www_example_com.crt
    SSLCertificateKeyFile /home/admin/certs/www_example_com.key
    SSLCACertificateFile /home/admin/certs/DigiCertCA.crt
    SSLEngine on
    SSLProtocol +TLSv1.2 -TLSv1.1 -TLSv1 -SSLv3 -SSLv2
    DirectoryIndex index.php index.html index.htm
    # redirect to www
    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

答案1

您忘記啟動 RewriteEngine。

    RewriteEngine on

相關內容