最近項目上需要泛二級域名解析,參考了網上的方案,然後結合自己的需求做了一個示例。需求如下:
1、我們的域名是domain.com,服務器用一個相同的IP地址;
2、www.domain.com 指向服務器中我們自己的index.aspx;
3、xxx.domain.com 指向服務器中客戶的site.aspx;
4、我們自己的頁面和客戶的頁面都在一個網站項目下;
具體做法如下:
1、引用微軟的URLRewriter項目,並修改其中BaseModuleRewriter.cs和ModuleRewriter.cs的源代碼;
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e) 方法中:
Rewrite(app.Request.Path, app);
修改爲:
Rewrite(app.Request.Url.AbsoluteUri, app);
protected override void Rewrite(string requestedPath, System.Web.HttpApplication app) 方法中:
string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
修改爲:
string lookFor = "^" + rules[i].LookFor + "$";
2、修改網站項目的web.config文件
添加:
<configSections>
</RewriterConfig>
在system.web節中加入
<httpModules>
3、修改域名DNS,將*.domain.com的域名指向服務器的IP地址;
4、配置IIS,去掉網站的主機頭,然後添加aspnet_isapi.dll的映射處理程序;
特別注意:如果是IIS7以上版本,請將網站的應用程序池類型修改爲“經典”;