修改UrlRewriter实现任意二级域名
例如 http://www.g.cn?id=1 可以利用正则表达式实现为 http://www.g.cn/1/ 等等!
但是 不可以实现为http://1.g.cn。
修改以下代码:
- BaseModuleRewriter.cs:
-
/// <summary>
-
/// Called when the module's AuthorizeRequest event fires.
-
/// </summary>
-
/// <remarks>This event handler calls the <see cref="Rewrite"/> method, passing in the
-
/// <b>RawUrl</b> and HttpApplication passed in via the <b>sender</b> parameter.</remarks>
-
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
-
{
-
HttpApplication app = (HttpApplication) sender;
-
Rewrite(app.Request.Path, app);
-
}
改为(红色的字体):
-
/// <summary>
-
/// Called when the module's AuthorizeRequest event fires.
-
/// </summary>
-
/// <remarks>This event handler calls the <see cref="Rewrite"/> method, passing in the
-
/// <b>RawUrl</b> and HttpApplication passed in via the <b>sender</b> parameter.</remarks>
-
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
-
{
-
HttpApplication app = (HttpApplication) sender;
-
// Rewrite(app.Request.Path, app);
-
// Change
-
Rewrite(app.Request.Url.AbsoluteUri, app);
-
}
- ModuleRewriter.cs
-
// 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) + "$";
改为:
-
string lookFor = "^" + rules[i].LookFor + "$";
最后Replease 生成,引用到使用的项目中。Web.Config:
- <configuration>下的<configSections>新增:
-
<configSections>
-
<!--配置动态二级域名设置begin-->
-
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>
-
<!--配置动态二级域名设置end-->
-
</configSections>
- <configuration>下的<system.web>新增:
-
<httpModules>
-
<!--URL重写-->
-
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
-
</httpModules>
然后 去你的域名管理者那边设置域名的泛解析。就是设置 * 。
配置IIS配置:在IIS\你的站点\属性\主目录\配置\映谢 在通配符应用程序配置处插入一个新的映谢。一般为:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll.
----------
到此,就完全可以实现自己想要的二级域名,无论多少!