0

I've tried numerous methods to get https://www.example.com (the url with www) to redirect to https://example.com (the url without www) but I keep getting a certificate warning. I have http:// redirecting to https:// just fine in the non-ssl vhost config.

The current version of the ssl vhost config looks like this (I've tried many variations but none work):

<IfModule mod_ssl.c>
        <VirtualHost *:443>
                ServerAdmin webmaster@localhost

                ServerName example.com
                DocumentRoot /var/www/html/website
                <Directory /var/www/html/website/>
                        AllowOverride All
                </Directory>

                ErrorLog ${APACHE_LOG_DIR}/error.log
                CustomLog ${APACHE_LOG_DIR}/access.log combined

                SSLEngine on

                SSLCertificateFile      /etc/apache2/ssl/fullchain.pem
                SSLCertificateKeyFile /etc/apache2/ssl/key.pem


                <FilesMatch "\.(cgi|shtml|phtml|php)$">
                                SSLOptions +StdEnvVars
                </FilesMatch>
                <Directory /usr/lib/cgi-bin>
                                SSLOptions +StdEnvVars
                </Directory>

                BrowserMatch "MSIE [2-6]" \
                                nokeepalive ssl-unclean-shutdown \
                                downgrade-1.0 force-response-1.0
                # MSIE 7 and newer should be able to use keepalive
                BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown

RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
        </VirtualHost>
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
5
  • 2
    The vHost you posted is only for example.com, not www.example.com? "but I keep getting a certificate warning" - do you have a security cert that covers the www subdomain? Does the www subdomain resolve?
    – MrWhite
    Nov 28, 2020 at 19:42
  • I've tried setting up a VirtuatHost for www and doing a Redirect permanent but it's just ignored. Cert was set up for both www and non-www (I believe, but I could be wrong). www resolves but I get a certificate warning.
    – StevieD
    Nov 28, 2020 at 19:52
  • 1
    "I've tried setting up a VirtuatHost for www and doing a Redirect permanent" - Well, that's exactly what you need to do (for both port 80 and 443). Please include what you have tried. Presumably you restarted Apache after making changes to the config?
    – MrWhite
    Nov 28, 2020 at 19:56
  • 2
    OK, got it working. Your comment got me to double check to see if I had issued the cert to www and indeed there was a bug in my setup script. I fixed the bug and reran my cert install script and now it's working. Thanks!
    – StevieD
    Nov 28, 2020 at 20:04
  • @StevieD: please write an answer explain what turned out to be the issue and how you fixed it. Or delete the question.
    – Tommiie
    Dec 3, 2020 at 10:15

0

You must log in to answer this question.

Browse other questions tagged .