solvit.io

Force HTTPS usage with JavaScript

Solution

First, detect if the current web page uses HTTPS. Then, if needed, redirect to the same page but replacing the original HTTP protocol by HTTPS:

<!DOCTYPE html>
<html>
<head>
    <title>Example.org</title>
    <script>
        if ('https:' != window.location.protocol) {
            window.location.href = 'https:' + window.location.href.substring(
                window.location.protocol.length
            );
        }
    </script>
</head>
<body>
    <!-- ... -->
</body>
</html>
Find more solutions about: , ,