Use Asp.net 2.0 to implement second-level domain names of websites

Source: Internet
Author: User
The following uses the domain name www. Domain Name. com as an example, so that each registered user has its own second-level domain name, where ABC. Domain Name. com is the second-level domain name to be used.

Basic Ideas:
1. Domain Name supports wildcard resolution, that is, resolve a record *. Domain Name. com to the server IP address, bind it to the server IIS, and the Host header is blank when binding;
2. to implement a complete second-level domain, create two sites, one for the master site, the other for the user, and both site directories refer to the same website directory.
3. In the WebProgramOr the Host header of the second-level domain name in the URL source, such as ABC in ABC. Domain Name. com;
4. Use the obtained second-level domain name and store it in the session for easy access
5. Use the obtained second-level domain name and URL to rewrite the address.

Implementation Method:
Domain name a record resolution Needless to say ^ _ ^, is to make a *. Domain Name. com a record resolved to your server IP

Method 1: second-level domain name URL redirection
A. Create a site and bind a domain name (win2003-IIS6) to IIS)
Open IIS, right-click the site, and click Properties. Click the Advanced button of the website item IP address, and click Edit or add to add a new binding. The host header value is blank, as shown in:

You only need to bind one, as shown in the following figure:

Set the default access document of this site to index. aspx ,.

After completion, No matter what level-2 prefix is used, the website will be accessed.

The following describes how to obtain the URL for analysis.CodeTo obtain and determine the Host Header. The code is placed in index. aspx. CS, the first file of the default document.

//


// obtain the second-level domain host header value, and switched to
//
Public void checkdomain ()
{< br> hostname = httpcontext. current. request. URL. host. tostring (); // obtain the URL host address
Userhost = hostname. split (New char [] {'. '}); // array, with ". separate

// Determine whether the second-level domain name address complies with the ABC. Domain Name. com format, and the length of the array Userhost is not greater than 3. Otherwise, the second-level domain name will jump to other pages.
If (Userhost. length> 3)
{
Httpcontext. Current. response. Redirect ("http: // www. Domain Name. com/error. aspx"); // jump to the error page
Return;
}

Userdomainname = Userhost [0]. tostring (); // obtain the first group of values in the array and the Host header of the second-level domain name

// Make specific judgments and do not use the Host Header as the second-level domain name
If (userdomainname. tolower () = "www" | userdomainname. tolower () = "Domain Name" | userdomainname = NULL | userdomainname. tostring () = "")
{
// Your actions
}
Else {
Httpcontext. Current. response. Redirect ("/user/"); // jump to the user directory, that is, the directory to which the second-level domain name is going. Of course, you can also jump to *. aspx? Userid = xxx
Return;
}

}

Now, ABC. Domain Name. com can be redirected to a specified page or link, but it is not a real second-level domain name, but a URL redirection.

Method 2: real second-level domain name
A. Create a site
At this point, we need to create two sites, one primary site and one second-level domain site. The file directories of the two sites are the same directory, and the directories include default. aspx and index. aspx. CreateCubeMethod:
A ). the method for creating a primary site is described in method 1. However, the host header is not empty and must be set to www. domain name. COM and domain name. com, of course, you can also set other host headers that do not want to be used as second-level domains. The default website access document is default. aspx.
B). The method for creating a second-level domain site is the same as that for creating a site in method 1. The default access document is index. aspx.

B. Now we need to use the previously established second-level domain site. We will store user data in the user directory under the root directory.
The following is the default document (index. aspx. CS) Process Code, mainly used to store the second-level domain name Host header into the session for convenient calling, is also the homepage file of the second-level domain (User zone)

//


// obtain the second-level domain host header value, stored in session ["userdomainname"]
//
Public void userdomainnamesession ()
{< br> hostname = httpcontext. current. request. URL. host. tostring (); // obtain the URL host address
Userhost = hostname. split (New char [] {'. '}); // array, with ". separate

// Determine whether the second-level domain name address complies with the ABC. Domain Name. com format, and the length of the array Userhost is not greater than 3. Otherwise, the second-level domain name will jump to other pages.
If (Userhost. length> 3)
{
Httpcontext. Current. response. Redirect ("http: // www. Domain Name. com // error. aspx"); // jump to the error page
Return;
}

Userdomainname = Userhost [0]. tostring (); // obtain the first group of values in the array and the Host header of the second-level domain name

// Make specific judgments and do not use the Host Header as the second-level domain name
If (userdomainname. tolower () = "www" | userdomainname. tolower () = "Domain Name" | userdomainname = NULL | userdomainname. tostring () = "")
{
// Your actions
}
Else
{
Httpcontext. Current. session ["userdomainname"] = userdomainname; // Save the Host header of the second-level domain name to the session
}
}
// Your processing of session ["userdomainname"]. For example, if the value of this session ["userdomainname"] is "ABC", you can index. aspx? Username = ABC. If you do not want to use session, you can use the URL address to obtain the Host header of the second-level domain.

C. URL rewriting
I am using Microsoft's urlrewriter. For more information, see [url] http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx? MFR = true [/url]
the rewrite method in Web. config is:


~ /Index \. aspx
~ /User/index. aspx

the second-level domain has been implemented here, regardless of the Host Header (www. domain name. COM and domain name. except for com, because these two domains have been bound to the primary site and are preferentially accessed to the primary site), ABC can be implemented. domain name. COM has accessed this user directory, and ABC appears in the address bar of the browser. domain name. to ensure that the second-level domain accesses other pages and maintains the attributes of the second-level domain name, you also need to rewrite the URL at the same time, if you need to use the second-level domain Host Header (User Name) for other pages, you can obtain it from session ["userdomainname"]. For example, you need to file test in the user directory. aspx: displays the Host header name of the second-level domain, and the address in the browser address bar must be: ABC. domain name. COM/test. aspx, then in the web. add URL rewriting rules in config:

~ /Test \. aspx
~ /User/test. aspx

then, test. aspx displays the Host header name of the second-level domain by obtaining the value of session ["userdomainname"] or by URL or fetch.
Of course, you can directly bind the second-level domain site to the user directory to obtain the Host Header (User Name) in the URL ), however, the convenience of data communication with the main site may be lost.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.