修改UrlRewriter实现任意二级域名

例如 http://www.g.cn?id=1 可以利用正则表达式实现为 http://www.g.cn/1/ 等等!

但是 不可以实现为http://1.g.cn

修改以下代码:

  • BaseModuleRewriter.cs:
Ericsth@Gmail.Com
  1. /// <summary>
  2. /// Called when the module's AuthorizeRequest event fires.
  3. /// </summary>
  4. /// <remarks>This event handler calls the <see cref="Rewrite"/> method, passing in the
  5. /// <b>RawUrl</b> and HttpApplication passed in via the <b>sender</b> parameter.</remarks>
  6. protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
  7. {
  8.     HttpApplication app = (HttpApplication) sender;
  9.     Rewrite(app.Request.Path, app);
  10. }

改为(红色的字体):

Ericsth@Gmail.Com
  1. /// <summary>
  2. /// Called when the module's AuthorizeRequest event fires.
  3. /// </summary>
  4. /// <remarks>This event handler calls the <see cref="Rewrite"/> method, passing in the
  5. /// <b>RawUrl</b> and HttpApplication passed in via the <b>sender</b> parameter.</remarks>
  6. protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
  7. {
  8.     HttpApplication app = (HttpApplication) sender;
  9.     // Rewrite(app.Request.Path, app);
  10.     // Change
  11.                    Rewrite(app.Request.Url.AbsoluteUri, app); 
  12. }
  • ModuleRewriter.cs
Ericsth@Gmail.Com
  1. // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
  2. string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";

改为:

Ericsth@Gmail.Com
  1. string lookFor = "^" + rules[i].LookFor + "$"; 

最后Replease 生成,引用到使用的项目中。Web.Config:

  • <configuration>下的<configSections>新增:
Ericsth@Gmail.Com
  1. <configSections>
  2.     <!--配置动态二级域名设置begin-->
  3.     <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>
  4.     <!--配置动态二级域名设置end-->
  5. </configSections>
  • <configuration>下的<system.web>新增:
Ericsth@Gmail.Com
  1. <httpModules>
  2.     <!--URL重写-->
  3.     <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
  4. </httpModules>

然后 去你的域名管理者那边设置域名的泛解析。就是设置 * 。

配置IIS配置:在IIS\你的站点\属性\主目录\配置\映谢 在通配符应用程序配置处插入一个新的映谢。一般为:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll.

----------

到此,就完全可以实现自己想要的二级域名,无论多少!

posted on 2010-01-15 22:59  Ericsth  阅读(567)  评论(2编辑  收藏  举报

导航