部署https(ssl)后设置301跳转将http跳转到https
linuxt系统 apache环境
云服务器:【直接在apache上部署的SSL】在对应站点根目录下新建(通过ftp或登录wdcp管理面板中:站点列表-文管-进入public_html-创建文件)一个文件命名为.htaccess。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:From-Https} !^on$ [NC]
#RewriteCond %{HTTPS} !^on$ [NC] # 非亚数机房用这一行替换上一行规则
RewriteCond %{HTTP_HOST} ^(www.)?abc.com$ [NC] # 将abc.com和www.abc.com跳转到https://www.abc.com,防止apache子站继承上级目录.htaccess受影响
RewriteRule ^(.*)$ https://www.xiuzhanwang.com/$1 [R=301,L]
</IfModule>
虚拟主机:可以通过ftp或登录后进入到主机管理面板-文件管理,进入wwwroot,新建一个文件命名为.htaccess文件,保存即可。
编辑.htaccess文件写入以下规则:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:From-Https} !^on$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?abc.com$ [NC] # 将abc.com和www.abc.com跳转到https://www.abc.com,防止apache子站继承上级目录.htaccess受影响
RewriteRule ^(.*)$ https://www.xiuzhanwang.com/$1 [R=301,L]
</IfModule>
Nginx环境
编辑nginx站点配置文件(登录wdcp管理面板中:站点列表-文管-虚拟主机站点文件nginx-对应站点配置文件),添加以下规则
server
{
listen 80;
server_name abc.com;
rewrite ^(.*) https://www.abc.com$1 permanent; # abc.com对应修改为您自已的域名
}
亚数机房CDN部署的SSL添加下面代码
if ( $http_from_https != 'on' ){
rewrite ^(.*) https://www.xiuzhanwang.com$1 permanent; # abc.com对应修改为您自已的域名
}
Windows系统 II7环境
云服务器:【直接在IIS上部署的SSL】在对应站点根目录下新建(通过ftp或登录后直接进入到D:\wwwroot\站点ftp命名目录\wwwroot创建)一个文件命名为web.config并编辑添加以下规则:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="301" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_FROM_HTTPS}" pattern="^on$" negate="true" />
<!-- <add input="{HTTPS}" pattern="^on$" negate="true" /> --> # 非亚数机房用这一行替换上一行规则
</conditions>
<action type="Redirect" url="https://www.xiuzhanwang.com/{R:1}" redirectType="Permanent" /> # www.abc.com对应修改为您自已的域名
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
虚拟主机:可以通过ftp或登录后进入到主机管理面板-文件管理,进入wwwroot,新建一个文件命名为web.config并编辑添加以下规则:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="301" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_FROM_HTTPS}" pattern="^on$" negate="true" />
</conditions>
<action type="Redirect" url="https://www.xiuzhanwang.com/{R:1}" redirectType="Permanent" /> # www.abc.com对应修改为您自已的域名
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
单独页面通用代码段:以下方法较适合指定某一个子页单独https
在需要强制为https的页面上加入以下代码进行处理http-->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>
在需要强制为http的页面上加入以下代码进行处理
https-->http
<script language="JavaScript" type="text/JavaScript">
function redirect()
{
var loc = location.href.split(':');
if(loc[0]=='https')
{
location.href='http:'+loc[1];
}
}
οnlοad=redirect
</script>
PHP页面跳转:添加在网站php页面内
http跳转https的方法较多,以上仅供参考。if ($_SERVER["HTTPS"] <> "on")
{
$xredir="https://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
header("Location: ".$xredir);
}