From 24d1ef99eebf10c0c017a1242da536e4f3d70ba5 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Thu, 21 Dec 2017 12:03:56 +0530 Subject: [PATCH 1/3] Snippet to perform HTTPS redirect --- snippets/httpsRedirect.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 snippets/httpsRedirect.md diff --git a/snippets/httpsRedirect.md b/snippets/httpsRedirect.md new file mode 100644 index 000000000..f60a79610 --- /dev/null +++ b/snippets/httpsRedirect.md @@ -0,0 +1,9 @@ +### httpsRedirect + +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. + +```js +const httpsRedirect = () => { + if(location.protocol !== "https:") location.replace("https://" + location.href.split("//")[1]); +} +``` \ No newline at end of file From 5c4a7f5bffcc1d600ec93ebe79a3b62cd8b0f1a4 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Thu, 21 Dec 2017 16:10:50 +0530 Subject: [PATCH 2/3] Update httpsRedirect.md --- snippets/httpsRedirect.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/snippets/httpsRedirect.md b/snippets/httpsRedirect.md index f60a79610..26f8ec2c9 100644 --- a/snippets/httpsRedirect.md +++ b/snippets/httpsRedirect.md @@ -2,8 +2,10 @@ 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.split()` and remove the protocol part of the URL. + ```js const httpsRedirect = () => { if(location.protocol !== "https:") location.replace("https://" + location.href.split("//")[1]); } -``` \ No newline at end of file +``` From 257efc2a00972bf421a2d983b02c960a8c53a39a Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 21 Dec 2017 14:26:15 +0200 Subject: [PATCH 3/3] Update httpsRedirect.md --- snippets/httpsRedirect.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/httpsRedirect.md b/snippets/httpsRedirect.md index 26f8ec2c9..b1b851320 100644 --- a/snippets/httpsRedirect.md +++ b/snippets/httpsRedirect.md @@ -6,6 +6,6 @@ Use `location.protocol` to get the protocol currently being used. If it's not HT ```js const httpsRedirect = () => { - if(location.protocol !== "https:") location.replace("https://" + location.href.split("//")[1]); + if(location.protocol !== "https:") location.replace("https://" + location.href.split("//")[1]); } ```