C# code - How to redirect a subdomain

Discussion in 'Site Programming, Development and Design' started by garthhenderson, Nov 13, 2009.

  1. Hiya folks,

    This post is related to the:
    "How to redirect a subdomain to a subdirectory"

    It is the syntax for C# programmers.

    This process is very easy using Expression Web. Of course you can substitute Expression Web with the FTP tool of your choice.

    On my Winhost root directory there is only a single default.aspx file and the mulitple subdirectories for my domains.

    Use the Expression Web menu option Site->Open Site to directly go to the Winhost root. Edit the Live Site.

    In the Site View tab, create your directories by right clicking on some open space in the Site View Contents (just like you would with Windows Explorer.

    Select New->Folder to create folders for all of your domain applications.

    Then create New->ASPX and name it default.aspx. Remove any files that are not needed on the root directory - keep it clean.

    Paste the following C# code into the body of the automatically generated code in default.aspx:

    NOTE: The Winhost forum seems to automatically strip out leading whitespace. The indentation shows in edit but does not display. It seems we have to use the "Go Advanced" format to indent lines.

    // As the Winhost KB suggests - always convert to upper case.
    string sn = Request.ServerVariables["SERVER_NAME"].ToUpper();

    // Put the domain name in CAPS - easier than adding the ToUpper()
    if ( sn.IndexOf("DOMAIN1") != -1 )

    // The directory could be any name you choose.
    // IIS treats the directory name as case insensitive but I recommend
    // matching the directory name case in the directory string.
    Response.Redirect("/domain1"); ​
    else if ( sn.IndexOf("DOMAIN2") != -1 )
    Response.Redirect("/domain2"); ​

    // Add as many else ifs as you need.

    I recommend putting the primary domain directory in a sub-directory for consistency.

    To add new domains to your existing site go to the:
    Winhost Control Panel -> Site -> Domain Pointer

    Simply add your additional domains as needed.

    I would be very interested to hear about improvements and/or alternate approaches.

    Last edited by a moderator: Oct 14, 2015
  2. Ray


    Garth thanks for the post. Expression web does make it a lot easier. But personally I like the IIS 7 URL Rewrite module to perform the redirects. I know the module is for rewriting URL's but it can perform redirects. And since the rule is placed in the applications web.config file I feel it's cleaner that way.

  3. Thanks, Ray. It's great to hear that the URL Rewrite works.

    Prior to going with the coding approach, I read through the URL Rewrite documentation and tried several variations without success. My tests worked worked but the rewrite didn't.

    I have IIS connected to my web host site - no problem there.

    Do you use the "Blank rule" or "User Friendly URL" config?

    Could you please post a screenshot of the IIS config screen or the XML syntax from the web.config file to accomplish the redirects?

    Are there any other required configurations other than the Winhost CP Domain Pointer to make the URL Rewrite work?
  4. I also want to know how to do this by just using URL Rewrite Module.
    I've tried for weeks how to redirect the subdomain, without coding.

    I still can't do it. I know I'm catching the string to catch, but it always points at the root directory. any ideas why?

    here are the steps i've done.

    1. check somewordhere.mydomain.com, it gives me an error (so i did step 2)
    2. add a record on the DNS manager (since it will not work id I don't add, right)
    3. tried to check somewordhere.mydomain.com now. it is accessible but points to root directory. (so i think i need to use URL rewriter now). also, it keeps the somewordhere.mydomain.com with a view of mydomain.com
    4. added redirect rule. I know it is working, tried and tested it.
    5. still points at mydomain.com root


    6. added domain pointer (somewords.mydomainipaddress)

    is there really a way to do this by using URL rewriter, and just do this...



  5. Ray


  6. yes, I did it in IIS 7 Manager.I'm able to do redirects until a requirement needs me to redirect catching the subdomain and redirecting it to some querystring..


    I'm able to do this, even by testing.. I can see what is on {R:1} and on {C:1}
    still it only goes to the root, making an infinite loop. (catch the subdomain, redirect {i dont know why it points to root) , catch subdomain, redirect ...loops)
  7. Ray


    It sounds like you have the pattern setup incorrectly. What is the expression you typed for the pattern in the URL Rewire module?
  8. Using the rewrite rule, here is how you can make your addon or subdomain point to a different folder.

    • Get IIS working with your hosting plan.
    • In IIS select "URL Rewrite"
    • Click "Add Rule(s)..."
    • Select "Blank rule"
    • With the drop down box change from using "Regular Expression" to "Wildcards".
    • Make your pattern "*" (without the quotes).
    • Click on conditions to make it expand.
    • Change the logical grouping drop down from "Match All" to "Match Any"
    • click "Add..."
    • use {HTTP_HOST} for condition input
    • for "Pattern", input your domain or subdomain for example: sub.site.com
    • Change your rewrite url to SUBDIRECTORY/{R:0}? replacing subdirector with whatever subdirectory you want.
  9. Ray


    Thanks for the post. I'm sure this will help some one.
  10. Hello,
    I created a site-connection succesffuly using II7 Manager, but I don't see File System Content under this connection node. Do you have any idea what's going on?

    See my detail post at: http://forum.Winhost.com/showthread.php?t=8498

    ALTKHA's post is promissing, but I don't even see any subfolders to proceed further. Help!
    Last edited by a moderator: Oct 14, 2015
  11. I got it figured out, no need to go through IIS Manager.

    One can skip step 5. Following Winhost's Application Starting Point procedure will do, then proceed with rerouting/rewriting-rules in web.config (step 6, lol) Thanks for the link to your blog anyway.

    But if anyone can help me with some tips on how to make application file system (all the folders/bin/subdir) of my site visible in IIS Manager, I would very much appreciate.
    Last edited by a moderator: Oct 14, 2015
  12. Ray


    Why do you need to see all these folders in your IIS 7 Manager. You can see them by clicking on Content View in IIS 7 Manager once you connected to our server, but keep in mind this should not be used as a way to upload or download files. IIS 7 Manager is not a file manager.
  13. Last edited by a moderator: Oct 14, 2015
  14. Here is my scenario, I've a main site in the root:

    I'm trying to use a sudirectory to have a subdomain:

    I've installed both in the root than in the subdir francocleopadre
    Blogengine.net , the subdir is an application starting point.

    I've tried the rewrite rule, but it seem that it have to be done using the web.config but how? I've read about avoidind inehrtiance in web.config but it says is a malformed web.config.
    In either case when it works the Subdomain modify directly the main domain and is not the way it have to be.

    I'm planning of installing other subdomains for other applications so I need to solve the issue.

    The two strange things are:
    1)I insert the rule trough the iss manager, click apply but then I don't see it listed.
    2)If I change paths in web.config of the subdir it crashes the subdir.

    What I'm missing?

    Thank You
  15. Last edited by a moderator: Oct 14, 2015

Share This Page