dreev
January 23, 2022, 12:50am
1
I’ve asked about related things before – Custom domains, www vs root, http vs https - #22 by dreev – but I have a small specific question:
Can I automatically redirect from the insecure, non-SSL, http version of my project’s URL to the secure https version?
I’ll need to figure this out for custom domains but maybe it’s easier to start with the case of redirecting from http://foo.glitch.me to https://foo.glitch.me …
Thanks for any insights!
PS: For quick reference, here’s what I’m now doing for static sites, thanks to @ordinary-mice :
<script>
if (window.location.protocol === 'http:')
window.location.replace('https'+window.location.href.slice(4))
</script>
1 Like
dreev
January 23, 2022, 6:26am
2
I don’t think this is an ideal solution but it seems to work to stick the following in the html header:
<script>
if (location.protocol !== 'https:') {
location.replace(`https:${location.href.substring(location.protocol.length)}`)
}
</script>
kael
January 23, 2022, 10:35am
3
I’m using this:
if (window.location.protocol === "http:")
window.location.protocol = "https:";
1 Like
dreev
January 23, 2022, 7:33pm
4
Well that’s much simpler! I guess “http” and “https” are the only protocols one will ever see these days so “if http then make it https” is quite sufficient. Thank you!
UPDATE: Wait, see below. The above breaks the back button on some browsers.
dreev
January 23, 2022, 8:16pm
6
Ah, thank you! For the no-backend case, is there any advantage to your version over @kael ’s above?
dreev
January 23, 2022, 8:36pm
8
The following project is currently using @kael ’s solution and it seems to not break the back button: http://oaks.glitch.me vs https://oaks.glitch.me
Maybe my particular browser is being clever about this (violating the anti-Postel principle perhaps?) but I should not rely on this?
dreev
January 23, 2022, 10:27pm
10
Sure enough, Chrome is stupidly being clever and masking the problem. In Firefox, @kael ’s solution breaks the back button. Confirmed that your solution works! Thank you again!
3 Likes
system
Closed
July 22, 2022, 10:27pm
11
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.