2

Ubuntu 14.04 Apache 2.4.18

I have set up a SSL certificate using letsencrypt. It works well but in order to work, I cannot have multiple vhost defined in one virtualhost configuration file. Each vhost has to sit in its own .conf file inside /etc/apache2/sites-available.

I need to make sure all requests to www.example.com are redirected to example.com. Since I need to solve it inside the unique virtualhost file, I'm trying to include a rewrite rule inside to perform the redirection but so far it fails silently: all https://www.example.com requests are not being redirected to https://example.com and a "insecure website" warning is displayed.

Here is the virtualhost configuration file for 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>
  • 2
    To whom it may concern: care to explain the downvote ? – pixeline Jan 13 '16 at 11:28
4

Since your SSL certificate is not valid for www.example.com, when someone accesses your site using www.example.com, they will get a certificate warning. This happens regardless of whether they are served a redirect or not. Otherwise, it would be possible to create a redirect to any site at all and it would look trustworthy.

What you need to do is to either get a certificate that covers both www.example.com and example.com, or to set up a separate virtualhost for www.example.com, with a valid certificate, and put the redirect in that virtualhost.

1
  • You cannot include a redirection http -> https rule inside a SSL-enabled virtualhost. You cannot by design. Just accept it. You need a plain-HTTP virtualhost for doing redirection from HTTP (and this is your main mistake).
  • Actually you can have just any config file inside your configuration directory in Debian-based Linux. You can even delete the dogmatic and uncomfortable sites-* directories and start using vhost.d directory, like I do. Nobody can force you use something that is uncomfortable.
  • I'm not sure I get your point: AFAICS I'm not setting up a redirection from http -> https, but from domain.com to domain.com. – pixeline Jan 13 '16 at 11:09
  • Do you test it accordingly ? And, anyway, what will happen if someone will just open your site (it will be opened via http). This is my point. – drookie Jan 13 '16 at 11:10
  • The http to https redirection is already set up and working, via a conf file listening to port 80. – pixeline Jan 13 '16 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.