You are here
Home > Web Development > WORDPRESS Devlopment > 301 redirect, www redirect, domain redirect through web.config

301 redirect, www redirect, domain redirect through web.config

First create a web.config file in your root directory if it’s not already there.  Second, put the following code into your web.config file.

Case1: 301 redirect domain.com to www.domain.com

<?xmlversion="1.0"encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rulename="Canonical Host Name"stopProcessing="true">
<matchurl="(.*)"/>
<conditions>
<addinput="{HTTP_HOST}"pattern="^domain\.com$"/>
</conditions>
<actiontype="Redirect"url="http://www.domain.com/{R:0}"redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Case2: 301 redirect all domains to one domain name

<?xmlversion="1.0"encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rulename="Enforce canonical hostname"stopProcessing="true">
<matchurl="(.*)"/>
<conditions>
<addinput="{HTTP_HOST}"negate="true"pattern="^www\.domain\.com$"/>
</conditions>
<actiontype="Redirect"url="http://www.domain.com/{R:0}"redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Leave a Reply

Top