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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u013089389/article/details/21184007

比如要把  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的通配符应用程序映射


展开阅读全文

没有更多推荐了,返回首页