ASP.NET UrlRewrite 实现 二级域名 重写

比如要把  http://www.aa.com/test.aspx?us=bbs   转换成 bbs.aa.com

 首先要做个域名解析,*.aa.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); 

//替换成  Rewrite(app.Request.Url.AbsoluteUri, app);   
        } 

2ModuleRewriter.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 = "^" + 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");
		}
	}

然后编译成BLL 上传到网站。

webconfig 添加 

<configSections>

<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>

</configSections> 添加
 <RewriterConfig>
  <!-- 处理默认首页失败-->
  <Rules>
   <RewriterRule>
    <LookFor>http://www/.aa/.com/</LookFor>
    <SendTo>~/default.aspx</SendTo>
   </RewriterRule>
  </Rules>
  <!-- 处理默认首页失败-->

  <!--二级域名正则-->
  <Rules>
   <RewriterRule>
    <LookFor>http://([a-zA-Z|0-9]+)/.aa/.com/</LookFor>
    <SendTo>/user/user.aspx?us=$1</SendTo>
   </RewriterRule>
  </Rules>
  <!--二级域名正则-->
 </RewriterConfig>

最后一步

IIS里面添加  ASPNET_ISAPI的通配符应用程序映射


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值