IIS 版本
IIs中實現Http自動轉換到Https方法介紹
1、根據IIS版本備份以下文件:
IIS6.0 路徑:C:\WINDOWS\Help\iisHelp\common\403-4.htm
IIS7.0以上 路徑:C:\inetpub\custerr\zh-CN\403.htm
2、把以下內容全部拷貝替換(403-4或403)裏面所有內容,保存即可
<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)”勾選上即可。
IIS7、8中,SSL設置-》把“要求SSL”勾選即可。
APache 版本
如果需要整站跳轉,則在網站的配置文件的<Directory>標籤內,鍵入以下內容:
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R]
複製代碼
如果對某個目錄做https強制跳轉,則複製以下代碼:
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來做跳轉!redirect 301 /你的網頁 https://你的主機+網頁
Tomcat 版本
需要做兩個地方改動。
1:server.xml 中配置ssl證書的端口要改成默認的“443”端口,如果已經修改,請直接操作第二步;
2:在web.xml配置文件中添加節點代碼:如下
<web-app>
.........
<security-constraint>
<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>
3:回到server.xml 配置文件中找到80端口的節點,裏面有默認這個屬性是 redirectPort="8443" 要改成 “443” 保存重啓即可。
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>
複製代碼
更多詳細參考SSL證書:www.evtrust.com