Redirect Rules with web.config

Redirect Rules with web.config

Similar to the ISAPI Rewrite Article we have written for our .htaccess users, we also have a list of common redirect and rewrite rules that will work for our web.config users. This article will work as your quick reference guide.

If you would like to use the .htaccess rewrite rules please see the following article:

Note: If placed in a sub-directory the rewrite rules will only take effect for that specific directory and any sub-directories beneath it.

Rewrite Samples

Note: The ‘Rule name’ value can be anything that you want. I recommend organizing your redirect rules by name to make it easier to manage. Especially, if there are a lot of them.

Redirecting To A Different Domain

If you need to redirect your website to another website.

<rewrite>
 <rules>
   <rule name="Imported Rule 1">
    <match url="^(.*)$" ignoreCase="false" />
     <conditions>
       <add input="{HTTP_HOST}" pattern="^(www\.)?domain\.com$" />
      </conditions>
     <action type="Redirect" redirectType="Permanent" url="http://www.newdomain.com/{R:1}"   appendQueryString="true" />
   </rule>
 </rules>

Redirecting To A Different Folder

Add the following code to direct your site to another directory.

<rewrite>
 <rules>
  <rule name="Imported Rule 1" stopProcessing="true">
   <match url="^oldfolder$" />
   <action type="Redirect" redirectType="Permanent" url="/correctfolder" />
  </rule>
 </rules>
</rewrite>

Redirecting File Names

To have your index.htm page auto redirect to index.asp user this example.

<rewrite>
 <rules>
   <rule name="Imported Rule 1" stopProcessing="true">
    <match url="index.htm" ignoreCase="false" />
    <action type="Redirect" redirectType="Permanent" url="index.asp" />
   </rule>
  </rules>
</rewrite>

SubFolder Rewrite

To redirect your domain to a subfolder of that domain example: www.domain.com to www.domain.com/folder

<rewrite>
   <rules>
    <!--# Exclude requests already going to /subfolder to avoid an infinite loop-->
     <rule name="Imported Rule 1" stopProcessing="true">
       <match url="^subfolder.*$" />
       <action type="None" />
     </rule>
    <!--# Rewrite normal requests to /subfolder-->
      <rule name="Imported Rule 2" stopProcessing="true">
        <match url="^(.*)$" ignoreCase="false" />
        <action type="Rewrite" url="/subfolder/{R:1}" />
      </rule>
   </rules>
 </rewrite>

NON-WWW. To WWW. Redirect

Redirecting non-www version to www., for example, domain.com to www.domain.com

<rewrite>
  <rules>
    <rule name="Imported Rule 1" stopProcessing="true">
       <match url="^" ignoreCase="false" />
         <conditions>
           <add input="{HTTP_HOST}" pattern="^$" ignoreCase="false" negate="true" />
           <add input="{HTTP_HOST}" pattern="^www\." negate="true" />
           <add input="{HTTPS}s" pattern="^on(s)|" ignoreCase="false" />
         </conditions>
       <action type="Redirect" redirectType="Permanent" url="http{C:1}://www.{HTTP_HOST}{URL}" />
     </rule>
   </rules>
</rewrite>

WWW. To NON-WWW. Redirect

Redirecting www version to non-www., for example, www.domain.com to domain.com

<rewrite>
  <rules>
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="^" ignoreCase="false" />
       <conditions>
        <add input="{HTTP_HOST}" pattern="^www.domain.com" ignoreCase="false" />
       </conditions>
      <action type="Redirect" redirectType="Permanent" url="http://domain.com{URL}" />
    </rule>
   </rules>
</rewrite>

URL Rewrite

Suppose you have URL like www.example.com/foo.asp?a=A&b=B&c=C and you want to access it as www.example.com/foo.asp/a/A/b/B/c/

<rewrite>
  <rules>
    <rule name="Imported Rule 1">
     <match url="^(.*?\.asp)/([^/]*)/([^/]*)(/.+)?" />
     <action type="Rewrite" url="{R:1}{R:4}?{R:2}={R:3}" appendQueryString="true" />
    </rule>
  </rules>
</rewrite> 

WordPress Permalinks using mod_rewrite are for Linux, but ISAPI_Rewrite does offer the equivalent. If you want to have index.php not show in the URL try using these in your .htaccess file.

If your WordPress site is in the wwwroot folder.

<rewrite>
  <rules>
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="." />
      <conditions>
       <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
       <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
      </conditions>
      <action type="Rewrite" url="/index.php" />
    </rule>
   </rules>
</rewrite>

If your WordPress site is in a subfolder.

<rewrite>
  <rules>
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="." />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
      </conditions>
      <action type="Rewrite" url="/subfolder/index.php" />
    </rule>
  </rules>
</rewrite>

Site Crawlers

Example on how to prevent certain spiders from crawling your site.

<rewrite>
  <rules>
    <rule name="Imported Rule 1">
      <match url=".*" ignoreCase="false" />
        <conditions>
          <add input="{HTTP_USER_AGENT}" pattern="^Baiduspider.*$" ignoreCase="false" />
        </conditions>
      <action type="Rewrite" url="/block.htm" />
    </rule>
    <rule name="Imported Rule 2">
      <match url=".*" ignoreCase="false" />
        <conditions>
          <add input="{HTTP_USER_AGENT}" pattern="^Yandex.*$" ignoreCase="false" />
        </conditions>
      <action type="Rewrite" url="/block.htm" />
    </rule>
  </rules>
</rewrite>

Variable URLs

Let’s say you want to have a URL display like: http://your_domain.com/some-folder/34-77-some-key-word.html But you want that to really process a query like: http://your_domain.com/folder/search.asp?country=34&city=77

<rewrite>
  <rules>
    <rule name="Imported Rule 1">
      <match url="^some-folder/([^-]+)-([^-]+)-.*$" ignoreCase="false" />
      <action type="Rewrite" url="/folder/search.asp?country={R:1}&amp;city={R:2}" appendQueryString="false" />
    </rule>
  </rules>
</rewrite>

Wild-Card Subdomains

Rewrite all wild-card sub-domain requests to a folder without affecting "your_domain.com" or "www.your_domain.com"

<rewrite>
  <rules>
    <!--# Ignore requests that are already rewritten-->
      <rule name="Imported Rule 1" stopProcessing="true">
        <match url="^subdomainfolder/.*$" />
        <action type="None" />
      </rule>
      <rule name="Imported Rule 2" stopProcessing="true">
        <match url="^(.*)$" ignoreCase="false" />
          <conditions>
           <!--# Rewrite all requests to non-www sub-domains to /subdomainfolder-->
            <add input="{HTTP_HOST}" pattern="^(www\.)?your_domain\.com$" negate="true" />
          </conditions>
        <action type="Rewrite" url="/subdomainfolder/{R:1}" />
      </rule>
  </rules>
</rewrite>

HTTP To HTTPS SSL Rewrites

Suppose you have URL like http://shop.example.com and you want your visitors to be redirected to https://shop.example.com

Here are some examples of how to force SSL. Simply place the following rules into your .htaccess file:

http To https redirect

This rule will detect if the request is incoming over the secure https protocol, if not it will force the request over the SSL port.

<rewrite>
   <rules>
    <!--# Redirect to HTTPS-->
     <rule name="Imported Rule 1">
       <match url="(.*)" ignoreCase="false" />
        <conditions>
          <!--## Redirect HTTP to HTTPS-->
          <!--# Only trigger rule if a non-ssl port is being used-->
           <add input="{SERVER_PORT}" pattern="443" ignoreCase="false" negate="true" />
        </conditions>
      <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
     </rule>
   </rules>
</rewrite>

http To https with WWW redirect

This version will be both redirected to HTTPS as well as add “www.” to the beginning of the hostname if it is missing.

<rewrite>
   <rules>
     <!--# Redirect to HTTPS with www-->
      <rule name="Imported Rule 1">
        <match url="(.*)" ignoreCase="false" />
          <conditions>
           <!--# Enable rewrite rules-->
           <!--## Redirect HTTP to HTTPS with www-->
           <!--# Only trigger rule if a non-ssl port is being used-->
             <add input="{SERVER_PORT}" pattern="443" ignoreCase="false" negate="true" />
           <!--# Extract non-www portion of HTTP_HOST-->
             <add input="{HTTP_HOST}" pattern="^(www\.)?(.*)" />
         </conditions>
       <action type="Redirect" redirectType="Permanent" url="https://www.{C:2}/{R:1}" />
     </rule>
   </rules>
</rewrite>