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. host headers of second-level domain names in Web programs or URLs, 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.
The following code is used to obtain and determine the Host Header. The code is placed in Index. aspx. cs, the first file of the default document.
Code:
/// <Summary>
/// Obtain the second-level domain host header value and perform redirection
/// </Summary>
Public void CheckDomain ()
{
HostName = HttpContext. Current. Request. Url. Host. ToString (); // obtain the URL Host address
UserHost = HostName. Split (new Char [] {'.'}); // array, separated "."
// 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. The method is as follows:
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)
Code:
/// <Summary>
/// Obtain the Host Header Value of the second-level domain and store it in Session ["UserDomainName "]
/// </Summary>
Public void UserDomainNameSession ()
{
HostName = HttpContext. Current. Request. Url. Host. ToString (); // obtain the URL Host address
UserHost = HostName. Split (new Char [] {'.'}); // array, separated "."
// 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 how to use it, see: http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx? Mfr = true
The rewrite method in web. config is as follows:
<! -- Rewrite the Host Header URL in the User area when abc. domain name. when accessing the site through com, the first file is Index by default. aspx, Index. override the aspx address to/User/Index. aspx -->
Code:
<RewriterRule>
<LookFor> ~ /Index \. aspx </LookFor>
<SendTo> ~ /User/Index. aspx </SendTo>
</RewriterRule>
The second-level domain has been implemented here, no matter what Host header is entered (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:
Code: <RewriterRule>
<LookFor> ~ /Test \. aspx </LookFor>
<SendTo> ~ /User/test. aspx </SendTo>
</RewriterRule>
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 also directly bind the second-level domain site to the User directory to obtain the Host Header (User Name) in the URL, but this may lose the convenience of data communication with the main site.
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. host headers of second-level domain names in Web programs or URLs, 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.
The following code is used to obtain and determine the Host Header. The code is placed in Index. aspx. cs, the first file of the default document.
Code:
/// <Summary>
/// Obtain the second-level domain host header value and perform redirection
/// </Summary>
Public void CheckDomain ()
{
HostName = HttpContext. Current. Request. Url. Host. ToString (); // obtain the URL Host address
UserHost = HostName. Split (new Char [] {'.'}); // array, separated "."
// 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. The method is as follows:
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)
Code:
/// <Summary>
/// Obtain the Host Header Value of the second-level domain and store it in Session ["UserDomainName "]
/// </Summary>
Public void UserDomainNameSession ()
{
HostName = HttpContext. Current. Request. Url. Host. ToString (); // obtain the URL Host address
UserHost = HostName. Split (new Char [] {'.'}); // array, separated "."
// 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 how to use it, see: http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx? Mfr = true
The rewrite method in web. config is as follows:
<! -- Rewrite the Host Header URL in the User area when abc. domain name. when accessing the site through com, the first file is Index by default. aspx, Index. override the aspx address to/User/Index. aspx -->
Code:
<RewriterRule>
<LookFor> ~ /Index \. aspx </LookFor>
<SendTo> ~ /User/Index. aspx </SendTo>
</RewriterRule>
The second-level domain has been implemented here, no matter what Host header is entered (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:
Code: <RewriterRule>
<LookFor> ~ /Test \. aspx </LookFor>
<SendTo> ~ /User/test. aspx </SendTo>
</RewriterRule>
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 also directly bind the second-level domain site to the User directory to obtain the Host Header (User Name) in the URL, but this may lose the convenience of data communication with the main site.
----------------------------
Everyone should know that Microsoft's URLRewrite can rewrite the URL, but also can only rewrite the part of the domain name after, rather than rewrite the domain name, such as: can rewrite the http://www.abc.com/1234/ to http://www.abc.com/show.aspx? Id = 1234 but cannot rewrite http://1234.abc.com to http://www.abc.com/show.aspx? Id = 1234.
To implement this function, the precondition is that www.abc.com is pan-parsed, and then URLRewriter needs to be modified.
2 files to be modified in total
1. BaseModuleRewriter. cs
Protected virtual void BaseModuleRewriter_AuthorizeRequest (object sender, EventArgs e)
{
HttpApplication app = (HttpApplication) sender;
Rewrite (app. Request. Path, app );
}
Change
Protected virtual void BaseModuleRewriter_AuthorizeRequest (object sender, EventArgs e)
{
HttpApplication app = (HttpApplication) sender;
Rewrite (app. Request. Url. AbsoluteUri, app );
}
Replace app. Request. Path with app. Request. Url. AbsoluteUri.
2. ModuleRewriter. cs
For (int I = 0; I <rules. Count; I ++)
{
// Get the pattern to look for, and Resolve the Url (convert ~ Into the appropriate directory)
String lookFor = "^" + RewriterUtils. ResolveUrl (app. Context. Request. ApplicationPath, rules [I]. LookFor) + "$ ";
// Create a regex (note that IgnoreCase is set)
Regex re = new Regex (lookFor, RegexOptions. IgnoreCase );
// See if a match is found
If (re. IsMatch (requestedPath ))
{
// Match found-do any replacement needed
String sendToUrl = RewriterUtils. ResolveUrl (app. Context. Request. ApplicationPath, re. Replace (requestedPath, rules [I]. SendTo ));
// Log rewriting information to the Trace object
App. Context. Trace. Write ("ModuleRewriter", "Rewriting URL to" + sendToUrl );
// Rewrite the URL
RewriterUtils. RewriteUrl (app. Context, sendToUrl );
Break; // exit the for loop
}
}
Change
For (int I = 0; I <rules. Count; I ++)
{
// Get the pattern to look for, and Resolve the Url (convert ~ Into the appropriate directory)
String lookFor = "^" + rules [I]. LookFor + "$ ";
// Create a regex (note that IgnoreCase is set)
Regex re = new Regex (lookFor, RegexOptions. IgnoreCase );
// See if a match is found
If (re. IsMatch (requestedPath ))
{
// Match found-do any replacement needed
String sendToUrl = RewriterUtils. ResolveUrl (app. Context. Request. ApplicationPath, re. Replace (requestedPath, rules [I]. SendTo ));
// Log rewriting information to the Trace object
App. Context. Trace. Write ("ModuleRewriter", "Rewriting URL to" + sendToUrl );
// Rewrite the URL
RewriterUtils. RewriteUrl (app. Context, sendToUrl );
Break; // exit the for loop
}
}
Set
String lookFor = "^" + RewriterUtils. ResolveUrl (app. Context. Request. ApplicationPath, rules [I]. LookFor) + "$ ";
Changed
String lookFor = "^" + rules [I]. LookFor + "$ ";
After completing these two changes, recompile the project and copy the generated dll to the bin directory.
After the modification, we COPY the UrlRewriter. dll to the Bin directory of our project. Is this the end? No.
First, make sure that your project has previously configured UrlRewriter as I wrote in the previous article. Otherwise, let's look back at the article.
If your project has been configured, we need to do the following:
1. Make sure that your domain name supports wildcard resolution. Then your website is the default website, otherwise it will not be implemented (at least I have not found a good solution)
2. In IIS configuration: in IIS \ your site \ properties \ main directory \ Configuration \ Insert a new in the configuration of the wildcard application. Set the executable file to the same configuration as the above ASPX page (Be sure not to check "check whether the file exists "). (It is used to process all requests through the ISAPI of asp.net. Only in this way can we rewrite all the addresses .)
3. Check your website host header. The first host header value must be blank; otherwise, an error occurs. You can add the domain name as needed to see how many domain names you want to bind.
4. Finally, rewrite your web. config file.
As mentioned in the previous section
<HttpHandlers>
<Add verb = "*" path = "*. aspx" type = "URLRewriter. RewriterFactoryHandler, URLRewriter"/>
<Add verb = "*" path = "*. html" type = "URLRewriter. RewriterFactoryHandler, URLRewriter"/>
</HttpHandlers>
Changed:
<HttpModules>
<Add type = "URLRewriter. ModuleRewriter, URLRewriter" name = "ModuleRewriter"/>
</HttpModules>
(Rewrite with the HTTP module instead of the HTTP program. Otherwise, the address must be overwritten .)
Then we can modify our rewrite RegEx:
<RewriterRule>
<LookFor> http: // (. [0-9] *) \. 178b2b \. com/</LookFor>
<SendTo> ~ /Search/Search_Sell.aspx? Id = $1 </SendTo>
</RewriterRule>
Well, now you can search for the appropriate category by entering the http://1.178b2b.com. But if you are smart, you will immediately find out. Dizzy, the home page cannot enter. Haha. Of course. You have to add a rewrite regular expression to the homepage.
<RewriterRule>
<LookFor> http: // www \. 178b2b \. com/</LookFor>
<SendTo> ~ /Index.htm </SendTo>
</RewriterRule>
Success. It feels so bad. Haha. Don't worry. If the second-level domain name points to the pictures connected to other pages with relative addresses under the directory, you have to be busy, you have to change all to the following method:
<A href = http://www.178b2b.com/cxlm/league.html target = "_ blank"> Integrity Alliance </a>
The preceding method uses UrlRewriter to implement second-level domain names. I hope everything goes smoothly.