各种平台下配置SSL自动跳转到HTTPS
Apache自动跳转到 HTTPS
网站根目录新建 .htaccess
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]301跳转
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R=301,L]
</IfModule>站点绑定多个域名,只允许www.kison.cn 跳转
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
###把网址更改为自己的###高级用法 (可选)
RewriteEngine on
# 强制HTTPS
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{SERVER_PORT} 80
# 某些页面强制
RewriteCond %{REQUEST_URI} ^something_secure [OR]
RewriteCond %{REQUEST_URI} ^something_else_secure
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
# 强制HTTP
RewriteCond %{HTTPS} =on [OR]
RewriteCond %{SERVER_PORT} 443
# 某些页面强制
RewriteCond %{REQUEST_URI} ^something_public [OR]
RewriteCond %{REQUEST_URI} ^something_else_public
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]IIS6 自动跳转到HTTPS
第一步: IIS6中,站点属性-》目录安全性-》编辑中把“要求安全通道(SSL)”勾选上即可。
第二步: 打开自己网站根目录, 例如 d:webroot, 在根目录新建一个名为 https.htm 的文件,内容如下:
<html>
<head><title>Redirecting...</title></head>
<script language="Javascript">
function redirectHttpToHttps()
{
var httpURL= window.location.hostname + window.location.pathname + window.location.search;
var httpsURL= "https://" + httpURL;
window.location = httpsURL;
}
redirectHttpToHttps();
</script>
<body>
</body>
</html>第三步: IIS6中, 站点属性 -》 自定义错误 -》选择 403.4 -》修改文件路径为 d:webroothttps.htm

IIS6 配置301跳转到https方法
一、确认已经配置好SSL证书
二、将主机头值修改一下,随便输入即可。这里以主机头为”ssl”为例。
三、新建一个站点,名称可自由设置,主机头填写为您的域名。
四、请将路径选择为您的网站目录,设置访问权限为只读即可。
五、创建好后站点后,选择该站点,点击鼠标右键--“属性”--“主目录”,按照下图设置,选中“重定向到URL”,重定向的地址是“https://域名$S$Q”并勾选“上面输入的准确URL”和“资源的永久重定向”两个选项即可。

IIS7 IIS8 自动跳转到HTTPS
IIS7需要先确认是否安装 “URL 重写” 或者 “URL Rewrite” 模块 , 如果您已经安装可以跳过
“URL重写” 模块下载地址
微软下载地址(64位):http://www.microsoft.com/zh-cn/download/details.aspx?id=7435
微软下载地址(32位):http://www.microsoft.com/zh-cn/download/details.aspx?id=5747
第一步: 选择站点, “URL 重写”,如果安装的是英文版的 应该是【Url Rewrite】
第二步: 添加 “ 空白规则”
第三步:添加规则

名称 : HTTPS
匹配URL 模式: (.*)
添加条件: 条件: {HTTPS} 模式: off
操作类型选择:重定向
重定向URL: https://{HTTP_HOST}/{R:1}
然后保存即可
高级版(直接把伪静态添加到web.config)
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>Nginx 自动跳转到HTTPS
方法一:
rewrite ^(.*)$ https://$host$1 permanent;
方法二:
适用于 80端口和443 配置在同一个 server{}内
if ($server_port = 80) {
rewrite ^(.*)$ https://$host$1 permanent;
}其他情况, 站点绑定很多域名,只让某个域名跳转:
set $redirect_https 1;
if ($server_port = 80) {
set $redirect_https "${redirect_https}2";
}
if ($http_host = 'www.gworg.com') {
set $redirect_https "${redirect_https}3";
}
if ($http_host = 'gworg.com') {
set $redirect_https "${redirect_https}3";
}
if ($redirect_https = "123") {
rewrite ^(.*)$ https://$host$1 permanent;
}配置示例:
server {
listen 80;
server_name www.gworg.com;
rewrite ^ https://$http_host$request_uri? permanent;
}
server {
listen 443;
ssl on;
ssl_certificate /etc/ssl/cacert.pem;
ssl_certificate_key /etc/ssl/privkey.pem;
server_name www.gworg.com;
server_tokens off;
location / {
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
}
}tomcat 自动跳转到HTTPS
把下面内容插入到 项目web.xml 文件中
<security-constraint> <web-resource-collection> <web-resource-name>Getssl</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint>
使用程序强制跳转到HTTPS
兼容:
<!-- 如果检测到是http页面,则自动跳转到对应的https页面 -->
<script type="text/javascript">
if (document.location.protocol != "https:") {
location.href = location.href.replace(/^http:/,"https:");
}
</script>ASP:
<%
If Request.ServerVariables("SERVER_PORT")=80 Then
Dim strSecureURL
strSecureURL = "https://"
strSecureURL = strSecureURL & Request.ServerVariables("SERVER_NAME")
strSecureURL = strSecureURL & Request.ServerVariables("URL")
Response.Redirect strSecureURL
End If
%>PHP:
if(!isset($_SERVER['HTTPS'])||!$_SERVER['HTTPS']){
$url ='https://'. $_SERVER['HTTP_HOST']. $_SERVER['REQUEST_URI'];
header('Location: '. $url);exit;
}
