2 or More Domain Names Linked to the Same Folder

Amgad Suliman | 01/02/2009 | 2 Comments | 34 Visits

Most if not all web hosting services allow users to link two or more domain names to the same account to save them the cost of a new account for each domain name. With such accounts, opening any of the registered domain names calls the same default page as the other. The solution is to write a Response.Redirect() call in the default page (usually Default.aspx) to redirect the user depending on the domain name entered.

The server variable SERVER_NAME contains the domain name part of the URL, so if the user entered ‘http://example.com/folder1/default.aspx’ its content will be ‘example.com’, and if the user entered ‘http://www.example.com/folder1/default.aspx’ its content will be ‘www.example.com’. To check for both versions always use the String object’s Contains() method, instead of checking for equality.

    protected void Page_Load(object sender, EventArgs e)
    {
        string domainName = Request.ServerVariables["SERVER_NAME"].ToString();
        domainName = domainName.ToLower();

        if (domainName.Contains("example.com"))
        {
            Response.Redirect("~/dotcom.aspx");
        }

        if (domainName.Contains("example.net"))
        {
            Response.Redirect("~/dotnet.aspx");
        }
    }
  • Share/Bookmark
Filed Under: ASP.NET, Mono XSP 2.0
Do you have an article you think webmasters might find useful? Send it to us.

Comments

  1. daian says:

    good….blog

  2. Thank you Daian very much.

Leave a Reply