Nowadays, ASP.NET virtual hosts can generally bind multiple domain names, but the pages opened through these domain names are the same. How to make these bound domain names open different pages respectively (that is, to realize the function of sub-websites)? It's actually very simple, just 4 steps:
1) Bind several domain names to the virtual host; for example: www.abc.com , services.abc.com, support.abc.com.
2) Create several folders in the root directory of the virtual host site; for example: services, support; do not create the www folder.
3) Under the vs 2005 Web project, create the same folders and add a default.aspx file under each folder; for example: services, support.
4) Add the Application_BeginRequest event in Global.asax: protected void Application_BeginRequest(object sender, EventArgs e)
{
string sumDomain;
string domain = Request.Url.Host;
// http://localhost does not have "."
int i = domain.IndexOf('.');
if (i > 0)
{
// Get the part before the first "." of the domain name (for example, www.abc.com) (excluding the first ".")
sumDomain = domain.Substring(0, i);
// If it is not "www", it will automatically redirect to http://www.abc.com/xxx ,
//The URL in the address bar will not display http://www.abc.com/xxx , but will display http://xxx.abc.com
if (sumDomain.IndexOf("www") == -1)
{
// Note, this sentence is the key
HttpContext.Current.RewritePath("~/" + sumDomain + Request.Url.PathAndQuery);
}
}
}
How about it, isn't it very simple! Haha, this is URL rewriting (HttpContext.Current.RewritePath).
URL of this article: http://www.cnblogs.com/anjou/archive/2006/12/23/601777.html