How to redirect from HTTP to HTTPS without breaking the back button. Article written By Mike Skeens. August 11, 2019.

Need to contact me? E-mail me at:

How to redirect from HTTP to HTTPS without breaking the back button

Say you want to redirect every page on your website. There are several ways of doing this, but only one right way to do so. Doing a search will show you the same stackoverflow result. The way they say to do it (which is the lame way!) is to do this in your pages header using JS (I prefer the PHP way, if not for the hosting redirect):

//redirect to https using PHP (you can add get methods this way!)
if ($_SERVER['HTTPS'] != "on") {
$url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
header("Location: $url");

//Here you can redirect from within a script page with any variables from previous page
header("Location: " . $_SESSION['uri'] . "?yourVariable=" .$theVariable ."&anotherVar=".$nextVar."&happyID=".$theGottenHappyID."#" . $anchor);

<!--redirect to https using javascript-->
//redirect to https with javascript
if (location.protocol !== "https:") {
location.protocol = "https:";}

Don't do it these ways because it will break your back button, unless you want it to be annoying, but people will then just get angry and close the page and walk away from you.

So here is the best method, and it's simple. Log in to your hosting service provider, go into your CPanel, and use an automatic redirect with the https version. So here in my example I redirect with TYPE: permament(301), DOMAINS: (don't use all, just the one you are redirecting within that website), SITE: (leave this one blank), REDIRECTS TO:

Bewm! That now makes your website redirect to https:// whenever someone types it with http:// or without any. This secures your site way better than redirecting it with refresh or javascript redirecing. Best of all, each page on your website now gets that green metal that google gives out for being good boy. OH! I forgot to mention that many host providers offer free SSL on all of your sites for free... Most of the major ones don't.

As you can see in the above picture, this is a redirect option with a servers customer CPanel options. The one pictured here is from A2hosting, but you could also do this from most any others like godaddy, etc.