A website without a secure certificate is no longer an option, so some time in your web development journey you’ll come across having to re-direct a domain that uses a secure certificate.
Recently I had some difficulty with this, possibly because both the old and new website was hosted on the same server. The client wanted to move the website from one domain to the other, i.e. “www.example.co.nz” to “www.example.com”, but she wanted both domains to be available. Easy, I thought! All I had to do was put a web-redirect on the domain.
Here’s the surprise though. That worked for redirecting “http://www.example.co.nz” to “https://www.example.com”, but not for “https://www.example.co.nz” to “https://www.example.com”.
I spent far too much time barking up the wrong tree on this. In the meantime, I’d disabled the hosting on the old website since I thought that it would no longer be needed.
Here’s the solution.
If you have a certificate, that will do the redirection and nowhere in the certificate does it say that it should redirect “https://www.example.co.nz” to “https://www.example.com”. So the solution is to redirect after the certificate has been read.
I re-enabled the hosting on the “www.example.co.nz” site, disabled the web redirect and changed the DNS to point back to the original site, i.e. I had two different sites, the old “www.example.co.nz” site, and the new “www.example.com” site. You have to have a valid certificate for both sites and I used “Let’s Encrypt” for that.
I now enabled the redirection in the .htaccess file instead of at the domain level. I entered the redirect at the beginning of the file:
RewriteRule ^(.*)$ https://www.example1.com/$1 [R=301,L]
The downside is that you have to have a host platform for both “www.example.co.nz” and “www.example.com”, so you have to pay for two hosting platforms. This way your site is redirected “after” the certificate is read and avoids any certificate errors.
Happy website, happy client!