利用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>

給TA打賞
共{{data.count}}人
人已打賞
源碼

极简云盘 – 简单便捷的纯PHP网盘程序

2017-3-19 17:00:42

源碼

给网站增加一个动态背景

2017-4-6 23:55:22

0 Reply AAuthor MManager
    暫無討論,說說你的看法吧
搜索