将wwwredirect到非www(apache,letsencrypt)

Ubuntu 14.04 Apache 2.4.18

我已经使用letsencrypt设置了一个SSL证书。 它工作正常,但为了工作,我不能在一个虚拟主机configuration文件中定义多个虚拟主机。 每个虚拟主机必须在/ etc / apache2 / sites-available中的自己的.conf文件中。

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

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

<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> 

由于您的SSL证书对www.example.com无效,因此当某人使用www.example.com访问您的网站时,他们将收到证书警告。 不pipe这些服务是否被redirect,都会发生这种情况。 否则,将有可能创build一个redirect到任何网站,它会看起来值得信赖。

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

  • 您不能在启用SSL的虚拟主机中包含redirecthttp – > https规则。 你不能通过devise。 只要接受它。 你需要一个简单的HTTP虚拟主机来做HTTPredirect(这是你的主要错误)。
  • 实际上,你可以在基于Debian的Linux中的configuration目录中find任何configuration文件。 你甚至可以删除教条和不舒服的sites-*目录,并开始使用vhost.d目录,就像我一样。 没有人可以强迫你使用不舒服的东西。