网站从http改成https(SSL证书)后设置301跳转将http跳转到https的方法

以下是qwbm.com当前网站使用的从http转到https的代码
使用环境介绍:用的是云主机(windows2008),为了优化与权重的考虑,采用了301转向(也可以不使用301转向,直接放在根目录就可以使用)

在根目录放一个 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.baidu.com/{R:1}" redirectType="Permanent" />
        </rule>
    </rules>
  </rewrite>
</system.webServer>
</configuration>



linux操作系统 apache环境:

云服务器:【直接在apache上部署的SSL】在对应站点根目录下新建(通过ftp或登录wdcp管理面板中:站点列表-文管-进入public_html-创建文件)一个文件命名为.htaccess。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:From-Https} !^on$ [NC]
#RewriteCond  %{HTTPS} !^on$ [NC]                        # 非亚数机房用这一行替换上一行规则
RewriteRule ^(.*)$ https://www.abc.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.abc.com$1 [R=301,L]
</IfModule>


虚拟主机:可以通过ftp或登录后进入到主机管理面板-文件管理,进入wwwroot,新建一个文件命名为.htaccess文件,保存即可。
编辑.htaccess文件写入以下规则:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:From-Https} !^on$ [NC]
RewriteRule ^(.*)$ https://www.abc.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.abc.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对应修改为您自已的域名   
}




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.abc.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.abc.com/{R:1}" redirectType="Permanent" />     # www.abc.com对应修改为您自已的域名
               </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>


Windows系统 II6环境:

参考http://www.west.cn/faq/list.asp?unid=650 配置一个Rewrite,编辑该Rewrite的规则文件httpd.conf 添加以下规则。

RewriteEngine On
RewriteCond %{HTTP:From-Https} !^on$ [NC]
#RewriteCond  %{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对应修改为您自已的域名


tomcat环境:

在web.xml最后一行</web-app>代码之前添加如下代码即可

<security-constraint>
    <!-- Authorization setting for SSL -->
    <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>

文章评论

Top