Well, I was not a 100% happy with the way the redirect was working when I initially set it up.
Any HTTP traffic heading to the site would get redirected to HTTPS but only if someone visited the link by using the naked domain ‘dipen.co.uk’.
Plus, I had a little bit of tinkering to do to the site as well. I wanted the ‘www.’ prefix working. Unfortunately if the ‘www.’ prefix was used, an error would appear. Something to do with Cloudflare but that is a topic for another post. By the way, if you don’t use Cloudflare, I highly recommend it! Link -> https://www.cloudflare.com/
I got the ‘www.’ prefix for the website working but the redirects were still not working as they should. If someone visited http://dipen.co.uk then it would redirect to https://dipen.co.uk but not to https://www.dipen.co.uk
Bit of searching and I came across this lovely snippet on StackOverflow:
1 2 3 4 5 6 7 8 |
RewriteCond %{HTTPS} off # First rewrite to HTTPS: # Don't put www. here. If it is already there it will be included, if not # the subsequent rule will catch it. RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # Now, rewrite any request to the wrong domain to use www. RewriteCond %{HTTP_HOST} !^www\. RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] |
Source: https://stackoverflow.com/questions/13977851/htaccess-redirect-to-https-www
It works like a charm! The balance in the universe has been restored! Yay!