location.protocol
to get the protocol currently being used.location.replace()
to replace the existing page with the HTTPS version of the page.location.href
to get the full address, split it with String.prototype.split()
and remove the protocol part of the URL.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
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️