不带www域名301到www

          <rule name="www" stopProcessing="true">
                <match url=".*" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^19yxw\.com$" />
                </conditions>
                <action type="Redirect" url="https://www.19yxw.com/{R:0}" />
            </rule>

http域名301到https

<rule name="redirect to HTTPS" stopProcessing="true">
 <match url="(.*)" />
 <conditions>
       <add input="{HTTPS}" pattern="^OFF$" />
 </conditions>
 <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
  </rule>

完整代码写入到web.config中

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <rewrite>
        <rules>
    <rule name="www" stopProcessing="true">
                <match url=".*" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^19yxw\.com$" />
                </conditions>
                <action type="Redirect" url="https://www.19yxw.com/{R:0}" />
            </rule>
            
           <rule name="redirect to HTTPS" stopProcessing="true">
               <match url="(.*)" />
                <conditions>
                <add input="{HTTPS}" pattern="^OFF$" />
                </conditions>
               <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
               </rule>
         </rules>
    </rewrite>   </system.webServer>  </configuration>

当web.config配置完成以后,可以通关网页状态码检测工具:http://tool.chinaz.com/pagestatus/查询返回的状态码是不是301,网上有些给出的代码,会出现当http跳转https时,出现302状态码(临时跳转),这是错误的,我们尽量使用301跳转
当我们在站长工具检查状态码,例如输入 19yxw.com 时,会出现301,此时实现的是 不带www的域名先向www域名301跳转,然后在实现http向https的301跳转,分2部执行;而当我们输入www.19yxw.com时,也会提示301,此时它实现的就是http向https的301重定向跳转

Last modification:July 22nd, 2021 at 05:20 pm
如果觉得我的文章对你有用,请随意赞赏