IIS 版本
Iis中實現Http自動轉換到Https方法介紹
修改如下文件:IIS6.0 路徑:C:\WINDOWS\Help\iisHelp\common\403-4.htm
IIS7.0以上 路徑:
C:\inetpub\custerr\zh-CN\403.htm
爲如下內容
<HTML><HEAD><TITLE>該頁必須經過安全通道查看</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=GB2312">
</HEAD><BODY>
<script type="text/javascript">
var url = window.location.href;
if (url.indexOf("https") < 0) {
url = url.replace("http:", "https:");
window.location.replace(url);
}
</script>
</BODY></HTML>
註釋:
IIS6中,站點屬性-》目錄安全性-》編輯中把「要求安全通道(SSL)」勾選上便可。
IIS七、8中,SSL設置-》把「要求SSL」勾選便可。
APache 版本
若是須要整站跳轉,則在網站的配置文件的<Directory>標籤內,鍵入如下內容: javascript
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R]
若是對某個目錄作https強制跳轉,則複製如下代碼: html
RewriteEngine on
RewriteBase /yourfolder
RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
若是隻須要對某個網頁進行https跳轉,能夠使用redirect 301來作跳轉! java
redirect 301 /你的網頁 https://你的主機+網頁 web
Tomcat 版本
須要作兩個地方改動。 安全
1:server.xml 中配置ssl證書的端口要改爲默認的「443」端口,若是已經修改,請直接操做第二步; app
2:在web.xml配置文件中添加節點代碼:以下 網站
<web-app> url
......... spa
<security-constraint> server
<web-resource-collection >
<web-resource-name >SSL</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
</web-app>
Nginx版本
在配置80端口的文件裏面,寫入如下內容便可。
server {
listen 80;
server_name localhost;
rewrite ^(.*)$ https://$host$1 permanent;
location / {
root html;
index index.html index.htm;
}
單獨頁面通用代碼段: 在須要強制爲https的頁面上加入該代碼進行處理 <script type="text/javascript"> var url = window.location.href; if (url.indexOf("https") < 0) { url = url.replace("http:", "https:"); window.location.replace(url); } </script> 以上僅供參考實現。