自动切换http为https重定向的几种有效方法,访问http网站页面时自动跳转https地址链接的代码,已测试

编程入门 行业动态 更新时间:2024-10-19 09:37:10

自动切换http为https重定向的<a href=https://www.elefans.com/category/jswz/34/1769370.html style=几种有效方法,访问http网站页面时自动跳转https地址链接的代码,已测试"/>

自动切换http为https重定向的几种有效方法,访问http网站页面时自动跳转https地址链接的代码,已测试

第一种 PHP 的自动切换方法:

<?php

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

?>

———————————————————————————————————————————

第二种 JS 的脚本自动跳转方法:

<script type="text/javascript">

var url = window.location.href;if (url.indexOf("https") < 0) { url = url.replace("http:", "https:");window.location.replace(url);}

</script>

———————————————————————————————————————————

第三种 Apache 的,写在.htaccess文件的<Directory>标签内即可:

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

———————————————————————————————————————————

第四种 Nginx 的,在配置文件nginx.conf中的 server { ... } 项中插入:

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

例如:

server {listen 80;server_name usenk;rewrite ^(.*)$ https://$host$1 permanent;}

或者写成新版的【 return 301 https://$server_name$request_uri;】也可以。

———————————————————————————————————————————

最后一种 HTML 的,在<head>标签中写个meta识别方法:

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

———————————————————————————————————————————

以上就是访问页面为http地址时自动切换https重定向的几种方法,感谢点赞关注轶软工作室。

更多推荐

自动切换http为https重定向的几种有效方法,访问http网站页面时自动跳转https地址链接的代码,已测试

本文发布于:2024-03-10 08:10:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1727435.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:几种   跳转   重定向   代码   页面

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!