Redirect permanent http to https

apache-2.4virtualhost

System: Ubuntu 14.04 , Apache 2.4.7

I want all requests to http://domain.com and https://domain.com be redirected to https://domain.com. I also want to redirect requests to the "www" subdomain to the main domain.com host, whilst a few other subdomains (tools.domain.com and phpmyadmin.domain.com) will stay accessible over http for the moment.

All this should be done directly via the virtualhost config file for domain.com, (/etc/apache2/sites-available/domain.com.conf full content here below), then restarted the apache2 service several times.

Redirection from http://domain.com to https://domain.com works.
Redirection from http://www.domain.com to https://domain.com does not work.
Redirection from https://www.domain.com to https://domain.com does not work.

Here is the virtualhost file content:

<VirtualHost *:*>
    ServerName www.domain.com
    Redirect permanent / https://domain.com/
</VirtualHost>

<VirtualHost _default_:80>
    ServerName domain.com
    Redirect permanent / https://domain.com/
    LogLevel error
</VirtualHost>

<VirtualHost *:80>
    ServerName tools.domain.com
    DocumentRoot /var/www/domain.com/subdomains/tools/public
</VirtualHost>

<VirtualHost *:80>
    ServerName phpmyadmin.domain.com
    DocumentRoot /usr/share/phpmyadmin
</VirtualHost>

<VirtualHost _default_:443>
    ServerName domain.com

    DocumentRoot /var/www/domain.com/public

    <Directory /var/www/domain.com/public>
        Require all granted
    </Directory>

    # SSL CERTIFICATES
    GnuTLSEnable on
    GnuTLSExportCertificates on
    GnuTLSCacheTimeout 500
    GnuTLSCertificateFile /etc/ssl/certs/domain.com-certificate-125023.crt
    GnuTLSKeyFile         /etc/ssl/private/domain.com.key
    GnuTLSPriorities      NORMAL
</VirtualHost>

Update: only https://www.domain.com does not work

Stupid me: the problem was beyond the server! I had not configured any 'www' subdomain in my DNS zone. I corrected that and now I'm almost there.

Best Answer

_default_ means default

With config like this:

<VirtualHost _default_:80>
    ...
</VirtualHost>

<VirtualHost _default_:443>
    ...
</VirtualHost>

<VirtualHost *:*>        
    ...
</VirtualHost>

The last virtual host is unreachable on ports 80 and 443 as any request on those ports will be processed by the relevant _default_ virtual host. The *:* virtual host probably isn't required at all (are you expecting public users to use more ports ??).

Working example

Therefore to have everything redirect to https://example.com you need, for example:

<VirtualHost *:443>
    ServerName example.com

    # SSL config

    ...
</VirtualHost>

<VirtualHost *:80>
    RewriteEngine On
    RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
</VirtualHost>

<VirtualHost *:443>

    # SSL config

    RewriteEngine On
    RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
</VirtualHost>

Note the use of a capture-everything rewrite rule, as using redirect only matches one url.