zoukankan      html  css  js  c++  java
  • JavaScript实现强制重定向至HTTPS页面

    有时候需要把网页强制切换成HTTPS,即使用户已经访问了HTTP的版本。原因可能是你不想让用户使用HTTP来访问,因为它不安全。要做到这个很简单,如果不想用PHP或者Apache的mod_rewrite来做这件事,用Javascript也可以。代码如下:

    <script type="text/javascript">
    var targetProtocol = "https:";
    if (window.location.protocol != targetProtocol)
     window.location.href = targetProtocol +
      window.location.href.substring(window.location.protocol.length);
    </script>

    用了这段代码,如果用户访问了如http://leonax.net/…,会被重定向到https://leonax.net/…..。如果想反过来,即把HTTPS强制重定向到HTTP,把targetProtocol的值改成http就行。是不是很方便?

  • 相关阅读:
    swift
    swift
    swift
    swift
    swift
    swift
    swift
    选择排序
    组合 和 继承
    Android中使用LitePal操控SQLite数据库
  • 原文地址:https://www.cnblogs.com/sunxiuwen/p/10082726.html
Copyright © 2011-2022 走看看