Merge pull request #286 from arjunmahishi/redirect
Snippet to perform HTTPS redirect
This commit is contained in:
11
snippets/httpsRedirect.md
Normal file
11
snippets/httpsRedirect.md
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
### 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.
|
||||||
|
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user