Httpd – Redirect particular hostname from https to httpd in httpd/apache2

apache-2.2httpdhttpsredirect

I have a webserver that has an ssl certificate applied to a subdomain https://shop.example.com. I also have the hostname http://example.com that has no ssl certificate. When invoking https://example.com, browsers issue a warning that a certificate could not be verified because the webserver is identifying itself as https://shop.example.com.

I would like all traffic that hits https://example.com to be redirected to http://example.com, and leave https://shop.example.com as is.

My httpd.conf file generally looks like this:

< VirtualHost 122.11.11.21:80 >
ServerName shop.example.com
.. regular old port 80 ..
< /VirtualHost >

< VirtualHost 122.11.11.21:443 >
ServerName shop.example.com
.. SSL applies here ..
< /VirtualHost >

< VirtualHost 122.11.11.21:80 >
ServerName example.com
.. regular old port 80 ..
< /VirtualHost >

It does not look as if I have SSL set up for https://example.com yet one can invoke SSL mode and the browser identifies the connection as https://shop.example.com. I need to redirect from https://example.com because for some reason, Google has indexed my website with this url even though it shows a warning.

I have tried various methods to get this to redirect and nothing has worked.

Any help would be greatly appreciated.

Best Answer

This was lifted up on the front page and I think the answer back from 2013 was based on an assumption that certificates are expensive and that OP can't buy one for example.com.

In this era of free Let's Encrypt certificates there's only one answer:

  1. Get a certificate for the redirection. (Getting Started with Let's Encrypt).

  2. Create a simple redirection, preferably using mod_alias Redirect.

    <VirtualHost *:443>
        ServerName example.com
    
        SSLEngine on
        SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    
        Redirect permanent / https://shop.example.com/
    </VirtualHost>