-3

My website can be referred to as example.co.uk and example.net with optional www at the front.

I am trying to set up Apache 2.4 so that if anyone lands on anything it always permanently redirects to https://www.example.co.uk.

My current configuration mostly works in that landing on any domain via http:// forwards properly to https://www.example.co.uk however typing any https://(www.)?example.net does not redirect to https://www.example.co.uk I just get the "Security risk" ahead message and it leaves me at the .net domain. Going to https://example.co.uk does properly redirect to https://www.example.co.uk.

Can someone give me a hint as to what is going on here or if there is a better way or even a way that does what I want!

Here is my complete configuration for the virtual domain:

<VirtualHost *:80>

        ServerName "www.example.co.uk:80"
        ServerAlias "www.example.net"
        ServerAlias "example.co.uk"
        ServerAlias "example.net"
        UseCanonicalName Off

        RewriteEngine On
        RewriteCond %{HTTP_HOST} !^www.example.co.uk$ [NC]
        RewriteRule ^(.*)$ https://www.example.co.uk$1 [L,R=301]

        ServerAdmin [email protected]
        DocumentRoot /home/xmpl/web/www

        ErrorLog /home/xmpl/logs/www/error.log
        CustomLog /home/xmpl/logs/www/access.log combined

</VirtualHost>

<VirtualHost *:443>

        ServerName "www.example.co.uk:443"
        ServerAlias "www.example.net"
        ServerAlias "example.co.uk"
        ServerAlias "example.net"
        UseCanonicalName Off

        RewriteEngine On
        RewriteCond %{HTTP_HOST} !^www.example.co.uk$ [NC]
        RewriteRule ^(.*)$ https://www.example.co.uk$1 [L,R=301]

        SSLEngine on
        SSLVerifyClient none
        SSLCertificateFile /home/xmpl/ssl/CertificateFile
        SSLCACertificateFile /home/xmpl/ssl/CertificateFileCA

        ProxyPass "/" "http://localhost:10300/"
        ProxyPassReverse "/" "http://localhost:10300/"

        ServerAdmin [email protected]
        DocumentRoot /home/xmpl/web/www

        ErrorLog /home/xmpl/logs/www/error.log
        CustomLog /home/xmpl/logs/www/access.log combined

</VirtualHost>

Updated My SSL certificate is a SAN cert for www.example.co.uk and admin.example.co.uk. Maybe it can't redirect because of that?

6
  • 1
    How did you created this configuration? That I'm flabbergasted how can up wuth this. There are so many examples of this online. And you made a couple of mistakes, the ServerName is incorrect and ServerName and ServerAlias should be without double quotes. Your also missing the private key in the ssl virtualhost. linuxize.com/post/redirect-http-to-https-in-apache. Also check here to generate a proper ssl virtualhost ssl-config.mozilla.org/…
    – Turdie
    Nov 4 at 19:29
  • 1
    @Turdie: SSLCertificateFile can contain the privatekey (though it's not recommended) and almost certainly does here. OP: yes if you want to redirect from (both) https://[www.]example.net then your cert must cover both those names. Nov 5 at 6:57
  • I copied most of the configuration from my old Plesk server which seemed very happy with generating and using double quotes with SN/SA as they are optional AFAIK. The :port was also copied from Plesk server. Yes @dave_thompson_085 the SSLCertificateFile contains the private key. If not recommended then what should I do instead? If I read your answers right then the config is fine and should work as is, I just need the .net name in the cert too? Nov 5 at 21:56
  • @Turdie "the ServerName is incorrect and ServerName and ServerAlias should be without double quotes." - There's nothing wrong with the ServerName used here and the double quotes are entirely optional.
    – MrWhite
    Nov 5 at 23:10
  • The recommendation is to use a separate file configured with SSLCertificateKeyFile; see httpd.apache.org/docs/2.4/mod/mod_ssl.html#sslcertificatefile . Yes I think it should work if you have all needed names in the cert -- if you want both bare example.net and www.example.net as desribed that requires two SAN entries. Nov 6 at 6:58

0

You must log in to answer this question.

Browse other questions tagged .