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.YourHostedDomainName.com --> http://subdomain1.YourHostedDomainName.com/subdomain1
http://subdomain2.YourHostedDomainName.com --> http://subdomain2.YourHostedDomainName.com/subdomain2
http://subdomain3.YourHostedDomainName.com --> http://subdomain3.YourHostedDomainName.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.YourHostedDomainName.com") ) > 0 Then
        Response.Redirect("/subdomain1")
ElseIf InStr( UCase(Request.ServerVariables("SERVER_NAME")), UCase("subdomain2.YourHostedDomainName.com") ) > 0 Then
        Response.Redirect("/subdomain2")
ElseIf InStr( UCase(Request.ServerVariables("SERVER_NAME")), UCase("subdomain3.YourHostedDomainName.com") ) > 0 Then
        Response.Redirect("/subdomain3/home.asp")
End If
%>


Note:
  1. Replace subdomain#.YourHostedDomainName.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.
If you run into problems when using this code, please post in our community forum. Technical support is unable to assist with specific coding issues.

Article ID: 369, Created On: 11/24/2008, Modified: 4/13/2010

Comments (1)

Willem Merifield

hi...dont forget to include the most important detail of all, namely...the 'UNLIMITED SUBDOMAIN' add-on must be enabled before the user even gets to this step. it took me 20 minutes of browsing through the 'knowledge base' and a pointless debug exercise on a simple script before i figured out the option was disabled on the account to begin with. may be a small thing but its important to cram as much important detail into an article. maybe you could summarize the topic answers and re-post one answer-per-group of similar questions so someone can get the full picture at 'FIRST GLANCE'. thanx

11/10/2011 at 9:56 PM