温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ASP.NET下用URLRewriter重写二级域名的步骤

发布时间:2021-09-01 11:02:51 来源:亿速云 阅读:152 作者:chen 栏目:编程语言

这篇文章主要介绍“ASP.NET下用URLRewriter重写二级域名的步骤”,在日常操作中,相信很多人在ASP.NET下用URLRewriter重写二级域名的步骤问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”ASP.NET下用URLRewriter重写二级域名的步骤”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

这里要求对域名进行重写,实现http://1234.abc.com/ 到 ~/Defa.aspx?id=1234的重写。

***:域名

首先域名要支持泛解悉,就是域名解悉的主机名为星号*,例:*.abc.com。如下图

ASP.NET下用URLRewriter重写二级域名的步骤


这样能保证你在浏览器地址栏输入任何前缀,DNS都会把它们指向到你指定的IP地址上。

第二:IIS设置(Win2003 + IIS 6为例)

(1)网站必须为Web服务器的默认站点,即端口号为80,主机头为空的站点。如下图所示。

ASP.NET下用URLRewriter重写二级域名的步骤


该站点接收所有对该服务器的HTTP请求(其它设置为主机头的站点除外)。所以任何二级域名访问该服务器都会由该站点进行处理。

(2)另外要在站点的“通配符应用程序映射”列表中添加ASP.NET的Web请求处理程序aspnet_isapi.dll。如下图所示。

ASP.NET下用URLRewriter重写二级域名的步骤


在这里的设置,是让该站点接到的所有请求都交给aspnet_isapi.dll处理。

第三:修改Microsoft的URLRewriter。

运行开源项目URLRewriter。这里需要修改两个地方:

(1)BaseModuleRewriter.cs类

 protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)    {    HttpApplication app = (HttpApplication) sender;    //Rewrite(app.Request.Path, app);   Rewrite(app.Request.Url.AbsoluteUri, app); // ## ## ## 这里修改了   }

这里将app.Request.Path 替换成了 app.Request.Url.AbsoluteUri

(2)ModuleRewriter.cs类

 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) + "$";    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 + "$";

这两个地方修改完以后,生成项目。将项止目bin/Debug目录下的URLRewriter.dll文件Copy到我们要重写URL的项目中。

第四:配置项目

(1)在web.config文件的configSections节点下添加如下一行代码

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

这里配置一个重写配置的类

(2)修改httpModules节点,在里面添加一行配置代码

<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />

(3)在主节点configuration节点下添加路由规则,代码如下:

 <!-- URL重写 将捕获页面转发到实际地址  -->     <RewriterConfig>       <Rules>              <RewriterRule>   <LookFor>http://(\w+).abc.com/</LookFor>  <SendTo>~/Defa.aspx?id=$1</SendTo>            </RewriterRule>   </Rules>   </RewriterConfig>   <!-- URL重写 将捕获页面转发到实际地址 ( 结束 )  -->

代码里一个RewriterRule节点就是一个规则,这时只有一个,即把域名中的主机头部分做为Defa.aspx页面的id参数的值发送给Defa.aspx页面。

注意:这里LookFor节点里的http://(\w+).abc.com/不能少了***的反斜框

OK,一切完工

发布,上传到服务器,测试一下,如图

ASP.NET下用URLRewriter重写二级域名的步骤

本次测试需要一个类库:URLRewriter.dll (测试版本 1.0.1495.18710)

到此,关于“ASP.NET下用URLRewriter重写二级域名的步骤”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI