@H_403_0@有时候需要把网页强制切换成HTTPS,即使用户已经访问了HTTP的版本。原因可能是你不想让用户使用HTTP来访问,因为它不安全。要做到这个很简单,如果不想用PHP或者<a target="_blank" href="//www.jb51.cc/article/67554.htm">Apache的mod_rewrite来做这件事,用Javascript也可以。代码如下:
<div class="jb51code">
<pre class="brush:js;">
<script type="text/javascript">
var targetProtocol = "https:";
if (window.location.protocol != targetProtocol)
window.location.href = targetProtocol +
window.location.href.substring(window.location.protocol.length);