Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Redirect http to https in html (well, javascript)

I wanted to redirect an http page to https, without changing virtual hostname. And I was able to find this on the Internet.

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
</body>
<script>
<!-- from https://stackoverflow.com/questions/4954768/automatic-redirection-to-https/4954784#4954784 -->
var loc = window.location.href+'';
if (loc.indexOf('http://')==0){
    window.location.href = loc.replace('http://','https://');
}
</script>
<footer>
</footer>
</html>

So as long as you reach this index.html, it will then redirect you to the webapp hosted only on the TLS-enabled virtual host.

Comments