将 www 重定向到非 www(apache、letsencrypt)

pix*_*ine 3 rewrite ssl-certificate apache-2.4 lets-encrypt

Ubuntu 14.04 Apache 2.4.18

我已经使用 letencrypt 设置了 SSL 证书。它运行良好,但为了工作,我不能在一个虚拟主机配置文件中定义多个虚拟主机。每个虚拟主机都必须位于 /etc/apache2/sites-available 内的自己的 .conf 文件中。

我需要确保对 www.example.com 的所有请求都重定向到 example.com。由于我需要在唯一的虚拟主机文件中解决它,我试图在里面包含一个重写规则来执行重定向,但到目前为止它无声地失败了:所有https://www.example.com请求都没有被重定向到https ://example.com并显示“不安全的网站”警告。

这是 domain.com-le-ssl.conf 的虚拟主机配置文件。

<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerName example.com
        ServerAlias www.example.com

        RewriteEngine on
        RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
        RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]

        DocumentRoot /home/user_account/www/example.com/public
        <Directory /home/user_account/www/example.com/public>
                Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

        ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/home/user_account/www/example.com/public/$1

SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
Run Code Online (Sandbox Code Playgroud)

Jen*_*y D 5

由于您的 SSL 证书对 www.example.com 无效,因此当有人使用 www.example.com 访问您的网站时,他们会收到证书警告。无论是否为它们提供重定向,都会发生这种情况。否则,完全可以创建到任何站点的重定向,并且看起来值得信赖。

您需要做的是获得一个涵盖 www.example.com 和 example.com 的证书,或者为 www.example.com 设置一个单独的虚拟主机,使用有效的证书,并将重定向放在该虚拟主机中.