I ran into some issues recently where I needed to redirect HTTP to HTTPS for all requests on my site. It took a surprising amount of Google searching and tinkering to find a solution. Ultimately the solution was pretty simple, so I figured I’d share it here and hopefully same other people some time.
So, backstory, I enabled SSL on one of the sites I run but when testing I noticed a few things needed to be fixed for the site to work 100% perfectly under HTTPS. I reached out to the web host and requested for the SSL (port 443) to be turned off for now until I could fix the issues. This request may as well have been asking for a trip to the moon.
The tech (three of them at different times actually) didn’t know how to do this. So I would up removing the SSL certificate via their control panel and figured that might do the trick. No, it didn’t. So I had them cancel the SSL “service” for me, hoping the system would be smart enough to turn off port 443 after it was canceled. No, it wasn’t.
So I went in search for a solution on my own. For about 20 years I ran all my sites on Windows – which is still an awesome solution – but currently my sites (running WordPress) are running on Linux – nginx specifically. Knowing this, I started looking for a solution using the htaccess file (similar to a .config file on Microsoft platforms). Even though I’m horrible at RegEx, I knew that redirecting from this file was an option. I just didn’t know how to check for SSL and what the rule should look like to do what I wanted.
Anyway… cutting to the chase… here are the lines I added to the htaccess file that redirected *all* HTTPS requests that my site received over to HTTP. It performs a 301 redirect to help with search engine tracking (passing “the juice” to the destination URL).
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Boom! Yep, it really was that simple. The first line checks to see if the request is via SSL and the second line redirects to the exact same URL except over a non-secure (HTTP) channel.
I hope that saves other people some time!