几种有效方法,访问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地址链接的代码,已测试
发布评论