Apache-2.4

Apache 不會強制 https://domain 到 https://www.domain(重寫不起作用)

  • March 27, 2022

設置 Apache 2.4 和 Wordpress 後,我收到以下 SSL 錯誤消息:

$ wget "https://example.com"
Resolving example.com (example.com)... 192.168.15.5
Connecting to example.com (example.com)|192.168.15.5|:443... connected.
ERROR: no certificate subject alternative name matches
requested host name ‘ example.com’.
To connect to example.com insecurely, use `--no-check-certificate'.

為了使域名與我的 SSL 證書 ( www.example.com) 匹配,我嘗試調整站點配置以將https://example.com>重定向到<https://www.example.com。但它不起作用:http-to-https 重定向功能如預期,但裸域到 www-domain 沒有。

這是我的站點配置:

&lt;VirtualHost *:80&gt;
       ServerName example.com
       ServerAlias www.example.com
       Redirect permanent / https://www.example.com/
&lt;/VirtualHost&gt;
                                                                                                 
&lt;IfModule mod_ssl.c&gt;
       &lt;VirtualHost _default_:443&gt;
           ServerName www.example.com

           Protocols h2 http/1.1

           ServerAdmin webmaster@localhost
           DocumentRoot /var/www/html
           DirectoryIndex index.php

           RewriteEngine on            #### Doesn’t work at all ####
           RewriteCond %{HTTP_HOST} ^example\.com [NC]
           RewriteRule ^/(.*) https://www.example.com/$1 [L,R=301]


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

           # SSL with Letsencrypt                                                                                                                Include /etc/letsencrypt/options-ssl-apache.conf
           SSLEngine on
           SSLCertificateFile      /etc/letsencrypt/live/www.example.com/fullchain.pem
           SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
                                                                            
   &lt;/VirtualHost&gt;
&lt;/IfModule&gt;

該站點本身執行良好,並且.htaccess僅包含由 Wordpress 創建的行。

任何輸入表示讚賞。

您仍然需要有效的證書http://example.com。重定向發生建立有效的 TLS 連接之後。

在 SAN 欄位中example.com獲取證書。www.example.com

引用自:https://serverfault.com/questions/1097110