Share via


IIS: most popular redirects using the configuration file (Web.config).

When publishing a website, you may need some redirects.

301 redirection

redirect from "HTTP" to "HTTPS" 

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <system.webServer>
 <rewrite>
 <rules>
 <rule name="Redirect to https" stopProcessing="true">
 <match url="(.*)" />
 <conditions>
 <add input="{HTTPS}" pattern="off" ignoreCase="true" />
 </conditions>
 <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
 </rule>
 </rules>
 </rewrite>
 </system.webServer>
</configuration>

redirect only main domain "domain.ltd" without any sub-domains

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <system.webServer>
 <rewrite>
 <rules>
 <rule name="Redirect to https" stopProcessing="true">
 <match url="(.*)" />
 <conditions>
 <add input="{HTTPS}" pattern="off" ignoreCase="true" />
 <add input="{HTTP_HOST}" pattern="^domain\.ltd" />
 </conditions>
 <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
 </rule>
 </rules>
 </rewrite>
 </system.webServer>
</configuration>

 redirect from "WWW" to "non-WWW" :

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
 <rules>
 <rule name="Imported Rule 1" stopProcessing="true">
 <match url="^(.*)$" ignoreCase="false" />
 <conditions logicalGrouping="MatchAll">
 <add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
 </conditions>
 <action type="Redirect" url="http://{C:1}/{R:1}" redirectType="Permanent" />
 </rule>
 </rules>
</rewrite>
</system.webServer>
</configuration>

redirect to another domain (e.g. redirect to domain.ltd) :

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <system.webServer>
 <rewrite>
 <rules>
 <rule name="301 Redirect 1" stopProcessing="true">
 <match url=".*" />
 <action type="Redirect" url="http://domain.ltd" redirectType="Permanent" />
 </rule>
 </rules>
 </rewrite>
 </system.webServer>
</configuration>

redirect from one page to another (e.g. redirect from test1.html to test2.html)

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <location path="test.html">
 <system.webServer>
 <httpRedirect enabled="true" destination="http://domain.ltd/test2.html" httpResponseStatus="Permanent" />
 </system.webServer>
 </location>
</configuration>

P.S. Important: Instead of the domain "domain.ltd" specify the name of your domain.