国内高防部署证书后自动301跳转https访问
发布时间:2021-05-20
注意:
1、规则里面的域名替换为实际域名
2、服务器内不需要开启部署HTTPS
3、宝塔面板不要开启强制HTTPS
apache环境:
在网站根目录下创建一个.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.abc.com/$1 [R=301,L]
</IfModule>Nginx环境:
编辑对应站点配置文件,在server配置节点中添加判断if这段配置
server
{
listen 80;
server_name abc.com;
}
#在网站server配置节中添加下面代码
if ( $http_from_https != 'on' ){
rewrite ^(.*) https://www.abc.com$1 permanent; # abc.com对应修改为您自已的域名
}Windows系统 IIS7及以上版本:
在网站根目录下创建web.config文件,写入一下代码。如果web.config本身就存在,在<system.webServer>配置节点后添加<rewrite>.......</rewrite> 这一段规则内容
<?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.abc.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>Windows2003系统 IIS6环境:
iis6的环境需要加载rewrite.dll组件,在httpd.ini或者httpd.conf里面添加一下规则
RewriteEngine On
RewriteCond %{HTTP:From-Https} !^on$ [NC]
RewriteRule ^(.*)$ https://www.abc.com/$1 [R=301,L] # www.abc.com对应修改为您自已的域名
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.abc.com$1 [R=301,L] # www.abc.com对应修改为您自已的域名