无法在 Apache 2.4.10 上将 https://www.example.com 重定向到 https://example.com

无法在 Apache 2.4.10 上将 https://www.example.com 重定向到 https://example.com

我尝试过很多方法来https://www.example.com(带 www 的 URL)重定向到https://example.com(不带 www 的 URL)但我一直收到证书警告。在非 SSL vhost 配置中,我已将 http:// 重定向到 https://。

ssl vhost 配置的当前版本如下所示(我尝试过许多变体,但都不起作用):

<IfModule mod_ssl.c>
        <VirtualHost *:443>
                ServerAdmin webmaster@localhost

                ServerName example.com
                DocumentRoot /var/www/html/website
                <Directory /var/www/html/website/>
                        AllowOverride All
                </Directory>

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

                SSLEngine on

                SSLCertificateFile      /etc/apache2/ssl/fullchain.pem
                SSLCertificateKeyFile /etc/apache2/ssl/key.pem


                <FilesMatch "\.(cgi|shtml|phtml|php)$">
                                SSLOptions +StdEnvVars
                </FilesMatch>
                <Directory /usr/lib/cgi-bin>
                                SSLOptions +StdEnvVars
                </Directory>

                BrowserMatch "MSIE [2-6]" \
                                nokeepalive ssl-unclean-shutdown \
                                downgrade-1.0 force-response-1.0
                # MSIE 7 and newer should be able to use keepalive
                BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown

RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
        </VirtualHost>
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

相关内容