This article explains how to add rewrite rules to your web.config file to redirect a subdomain to a directory under your main website.

Web.config files work with Ecenica Windows hosting and Windows Reseller hosting

Example

Insert the code below between <system.webServer> and </system.webServer> element of your web.config.xml.

Redirect a .co.uk, .org.uk, .me.uk subdomain

<rewrite>
   <rules>
      <rule name="Redirect subdomain" enabled="true">
         <match url="^(.*)$" />
         <conditions>
            <add input="{HTTP_HOST}" pattern="^sub\.example\.co\.uk$" />
         </conditions>
         <action type="Rewrite" url="folder/{R:1}" />
      </rule>
   </rules>
</rewrite>

Redirect a .com, .org, .net subdomain

<rewrite>
   <rules>
      <rule name="Redirect subdomain" enabled="true">
         <match url="^(.*)$" />
         <conditions>
            <add input="{HTTP_HOST}" pattern="^sub\.example\.com$" />
         </conditions>
         <action type="Rewrite" url="folder/{R:1}" />
      </rule>
   </rules>
</rewrite>

Full web.config Example

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <security>
            <authentication>
                <basicAuthentication enabled="false" />
            </authentication>
        </security>
        <httpProtocol>
            <customHeaders>
                <clear />
                <add name="X-Powered-By" value="ASP.NET" />
            </customHeaders>
        </httpProtocol>
        <directoryBrowse enabled="false" />
        <defaultDocument>
            <files>
                <clear />
                <add value="Default.htm" />
                <add value="Default.asp" />
                <add value="index.asp" />
                <add value="index.htm" />
                <add value="index.html" />
                <add value="index.php" />
                <add value="Default.aspx" />
            </files>
        </defaultDocument>
            <rewrite>
               <rules>
                   <rule name="Redirect subdomain" enabled="true">
                       <match url="^(.*)$" />
                           <conditions>
                               <add input="{HTTP_HOST}" pattern="^sub\.example\.co\.uk$" />
                           </conditions>
                       <action type="Rewrite" url="folder/{R:1}" />
                   </rule>
             </rules>
           </rewrite>
    </system.webServer>
</configuration>