強制跳轉 https 的幾種方法


html 文件,head 中加入如下meta

<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

javascript 腳本,適用於IIS服務器的 403-4.htm 或者 403.htm

<script type="text/javascript">
var url = window.location.href;
if (url.indexOf("https") < 0) {
    url = url.replace("http:", "https:");
    window.location.replace(url);
}
</script>

php 腳本

if ($_SERVER["HTTPS"] <> "on"){
    $xredir="https://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    header("Location: ".$xredir);
}

.htaccess文件,適用於Apache 配置文件的<Directory>標簽內

RewriteEngine on
RewriteBase /yourfolder
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

Nginx服務器,在 server {  ... } 中插入

    rewrite ^(.*)$  https://$host$1 permanent;

 

參考鏈接 https://www.sslzhengshu.com/article/post-447.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM