How to redirect a subdomain to a subdirectory

You can redirect a subdomain to a subdirectory by using ASP or ASP.net code.

When the requests are redirected using an ASP file, the URL in browser for redirected domains will show the correct domain name and the directory name where the request is being redirected. You can also redirect the requests to a specific file.

For example:

http://subdomain1.HostingAccountDomain.com --> http://subdomain1.HostingAccountDomain.com/subdomain1

http://subdomain2.HostingAccountDomain.com --> http://subdomain2.HostingAccountDomain.com/subdomain2

http://subdomain3.HostingAccountDomain.com --> http://subdomain3.HostingAccountDomain.com/subdomain3/home.asp


The following is a sample script that you can use to redirect a subdomain. You will need to place it as default document on your document root.
 
<% 
If InStr( UCase(Request.ServerVariables("SERVER_NAME")),  UCase("subdomain1.HostingAccountDomain.com") ) > 0 Then 
        Response.Redirect("/subdomain1") 
ElseIf InStr( UCase(Request.ServerVariables("SERVER_NAME")), UCase("subdomain2.HostingAccountDomain.com") ) > 0 Then 
        Response.Redirect("/subdomain2") 
ElseIf InStr( UCase(Request.ServerVariables("SERVER_NAME")), UCase("subdomain3.HostingAccountDomain.com") ) > 0 Then 
        Response.Redirect("/subdomain3/home.asp") 
End If 
%> 
Note:
  1. Replace subdomain.HostingAccountDomain.com with your actual subdomain URL.
  2. Replace /subdomain with your actual subdirectory name.
  3. In the last redirection statement in the example, we show how to redirect to a specific file.
 
Second Option: Using URL Rewrite rules:
 
By default all subdomain names point to the root of site account. This means you would need to place the following rules within the root's web.config file of your site account.
 
<rewrite>
    <rules>
        <rule name="subdomain.hostingaccountdomain.com" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^subdomain.hostingaccountdomain.com$" />
                <add input="{PATH_INFO}" pattern="^/subdirectory/" negate="true" />
            </conditions>
            <action type="Rewrite" url="\subdirectoryname\{R:0}" />
        </rule>
    </rules>
</rewrite>
Please note the above URL Rewrite code is from Scott Forsyth's Blog
 
 
If you run in to problems using this code, please post in our community forum. Technical support cannot assist with specific coding related issues.