本文转自:http://blog.csdn.net/suyiming/archive/2009/02/28/3944537.aspx
微软的URLRewrite能够对URL进行重写,但是也只能对域名之后的部分进行重写,而不能对域名进行重写,如:可将 http://www.worldbao.com/showuser.aspx?us=suyiming 重写为 http://www.worldbao.com/suyiming.aspx 但不能将 http://www.worldbao.com/show.aspx?us=suyiming 重写为 http://suyiming.worldbao.com/
要实现这个功能,首先要做域名泛解析,去域名管理里面的域名解析中添加一个:*.worldbao.com 指向服务器ip。
第二,重写URLRewrite里面的两个方法。
1.BaseModuleRewriter.cs 里面的BaseModuleRewriter_AuthorizeRequest方法
将
- protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
- {
- HttpApplication app = (HttpApplication) sender;
- Rewrite(app.Request.Path, app);
- }
- protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
- {
- HttpApplication app = (HttpApplication) sender;
- Rewrite(app.Request.Path, app);
- }
改为
- protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
- {
- HttpApplication app = (HttpApplication) sender;
- Rewrite(app.Request.Url.AbsoluteUri, app);
- }
- protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
- {
- HttpApplication app = (HttpApplication) sender;
- Rewrite(app.Request.Url.AbsoluteUri, app);
- }
2, ModuleRewriter.cs 里面的 Rewrite 方法
将
- protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
- {
- // log information to the Trace object.
- app.Context.Trace.Write("ModuleRewriter", "Entering ModuleRewriter");
- // get the configuration rules
- RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;
- // iterate through each rule...
- 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
- }
- }
- // Log information to the Trace object
- app.Context.Trace.Write("ModuleRewriter", "Exiting ModuleRewriter");
- }
- }
- protected override void Rewrite(string requestedPath,
- System.Web.HttpApplication app) { // log information to the Trace
- object. app.Context.Trace.Write("ModuleRewriter", "Entering
- ModuleRewriter"); // get the configuration rules RewriterRuleCollection
- rules = RewriterConfiguration.GetConfig().Rules; // iterate through
- each rule... 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 } } // Log information to the
- Trace object app.Context.Trace.Write("ModuleRewriter", "Exiting
- ModuleRewriter"); } }
改为
- protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
- {
- // log information to the Trace object.
- app.Context.Trace.Write("ModuleRewriter", "Entering ModuleRewriter");
- // get the configuration rules
- RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;
- // iterate through each rule...
- 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
- }
- }
- // Log information to the Trace object
- app.Context.Trace.Write("ModuleRewriter", "Exiting ModuleRewriter");
- }
- }
- protected override void Rewrite(string requestedPath,
- System.Web.HttpApplication app) { // log information to the Trace
- object. app.Context.Trace.Write("ModuleRewriter", "Entering
- ModuleRewriter"); // get the configuration rules RewriterRuleCollection
- rules = RewriterConfiguration.GetConfig().Rules; // iterate through
- each rule... 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 } } // Log information to the
- Trace object app.Context.Trace.Write("ModuleRewriter", "Exiting
- ModuleRewriter"); } }
就是将
string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
改成了
string lookFor = "^" + rules[i].LookFor + "$";
完成这过程之后将整个项目重新编译一下。
这些都做完之后,
在webconfig配置里面的
<configSections>里面 添加:
- <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>
- <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>
在</configSections>下面添加
- <RewriterConfig>
- <!-- 处理默认首页失败-->
- <Rules>
- <RewriterRule>
- <LookFor>http://www\.worldbao\.com/</LookFor>
- <SendTo>~/default.aspx</SendTo>
- </RewriterRule>
- </Rules>
- <!-- 处理默认首页失败-->
- <!--二级域名正则-->
- <Rules>
- <RewriterRule>
- <LookFor>http://([a-zA-Z|0-9]+)\.worldbao\.com/</LookFor>
- <SendTo>/user/user.aspx?us=$1</SendTo>
- </RewriterRule>
- </Rules>
- <!--二级域名正则-->
- </RewriterConfig>
- <RewriterConfig>
- <!-- 处理默认首页失败-->
- <Rules>
- <RewriterRule>
- <LookFor>http://www\.worldbao\.com/</LookFor>
- <SendTo>~/default.aspx</SendTo>
- </RewriterRule>
- </Rules>
- <!-- 处理默认首页失败-->
- <!--二级域名正则-->
- <Rules>
- <RewriterRule>
- <LookFor>http://([a-zA-Z|0-9]+)\.worldbao\.com/</LookFor>
- <SendTo>/user/user.aspx?us=$1</SendTo>
- </RewriterRule>
- </Rules>
- <!--二级域名正则-->
- </RewriterConfig>
在httpModules里面添加
- <httpModules>
- <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
- </httpModules>
- <httpModules>
- <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
- </httpModules>
最后一步
IIS里面添加 ASPNET_ISAPI的通配符应用程序映射
操作方法:IIS站点属性 ->主目录 -> 配置
点击插入按键
选择或输入C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll
取消"确认文件是否存在"前的钩. 确定