Created
February 12, 2019 02:43
-
-
Save stevenbeales/fa818c334e86b8625d24f1607fc878ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const httpsRedirect = () => { | |
if (location.protocol !== 'https:') location.replace('https://' + location.href.split('//')[1]); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
httpsRedirect(); // If you are on http://mydomain.com, you are redirected to https://mydomain.com | |
Redirects the page to HTTPS if its currently in HTTP. Also, pressing the back button doesn't take it back to the HTTP page as its replaced in the history. | |
Use location.protocol to get the protocol currently being used. If it's not HTTPS, use location.replace() to replace the existing page with the HTTPS version of the page. Use location.href to get the full address, split it with String.prototype.split() and remove the protocol part of the URL. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment