web.config如何实现整站301跳转?或者单独页面301?


.htaccess的301定向非常简单,那么web.config的301定向又应该怎么实现呢?

先来看下,web.config中的301格式

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect(命名)" stopProcessing="true">
<match url="^(要重定向的页面)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="(重定向到的页面)" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

例1:多页面跳转

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect" stopProcessing="true">
<match url="^notes/dede301.html" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="http://www.dedeking.com/notes/webconfig301.html" />
<rule name="Redirect2" stopProcessing="true">
<match url="^notes/dede302.html" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="http://www.dedeking.com/notes/webconfig302.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

多个页面跳转时,rule name 不能相同

例2:整站301跳转

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="301Redirect" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^dedeking.com$" />
</conditions>
<action type="Redirect" url="http://www.dedeking.com/{R:0}"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

例3:整站301和单独页面301

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
 
<rule name="Redirect" stopProcessing="true">
<match url="^notes/dede301.html" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="http://www.dedeking.com/notes/webconfig301.html" />
</rule>
 
<rule name="301Redirect" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^dedeking.com$" />
</conditions>
<action type="Redirect" url="http://www.dedeking.com/{R:0}"
redirectType="Permanent" />
 
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

就是如此简单,你学会了吗?