HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

How can I redirect the page to HTTPS in JavaScript?

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascripthowredirectthecanhttpspage

Problem

HTTP is a highly insecure and legacy protocol, and it's recommended to use HTTPS instead. For a few years now, browsers have been marking HTTP sites as insecure and even blocking certain features on them. If you have a website that's still using HTTP, it's a good idea to redirect it to HTTPS.
But how exactly can you do that using JavaScript? You can easily get the current protocol via the use of location.protocol. If it's not HTTPS, you can use location.replace() to replace the existing page with the HTTPS version of the page. In order to get the full address, you can use location.href, split it with String.prototype.split() and replace the protocol part of the URL.
Note that, when using this method, pressing the back button doesn't take it back to the HTTP page as it's replaced in the history.

Solution

const httpsRedirect = () => {
  if (location.protocol !== 'https:')
    location.replace('https://' + location.href.split('//')[1]);
};

httpsRedirect();
// If you are on http://mydomain.com,
//   you are redirected to https://mydomain.com


Note that, when using this method, pressing the back button doesn't take it back to the HTTP page as it's replaced in the history.

Code Snippets

const httpsRedirect = () => {
  if (location.protocol !== 'https:')
    location.replace('https://' + location.href.split('//')[1]);
};

httpsRedirect();
// If you are on http://mydomain.com,
//   you are redirected to https://mydomain.com

Context

From 30-seconds-of-code: https-redirect

Revisions (0)

No revisions yet.