javascript中http到https协议的更改 如果(location.protocol!=“https:”) { location.href='https:'+window.location.href.substring(window.location.protocol.length); } 你好

javascript中http到https协议的更改 如果(location.protocol!=“https:”) { location.href='https:'+window.location.href.substring(window.location.protocol.length); } 你好,javascript,Javascript,我需要更改url中的协议。我将sample.html放在ApacheHtdocs中。我正在使用http将其更改为https,但无法使用https访问该站点。有谁能为我提供解决方案吗如果你想让你的站点始终重定向到HTTPS版本,你应该通过你的服务器配置,而不是JavaScript来实现。你能告诉我怎么做吗?你使用apache还是nginx?我使用的是@baao apache <html> <head> <script src="jquery-3.1.1

我需要更改url中的协议。我将sample.html放在ApacheHtdocs中。我正在使用http将其更改为https,但无法使用https访问该站点。有谁能为我提供解决方案吗

如果你想让你的站点始终重定向到HTTPS版本,你应该通过你的服务器配置,而不是JavaScript来实现。你能告诉我怎么做吗?你使用apache还是nginx?我使用的是@baao apache
<html>
    <head>
    <script src="jquery-3.1.1.min.js"></script>
    <script>
     if (location.protocol != 'https:')
        {
         location.href = 'https:' + window.location.href.substring(window.location.protocol.length);
        }
    </script>
    </head>
    <body>
            <span class="sample">hi</span>
    </body>
</html>