Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Steps to rewrite a second-level domain name with URLRewriter under ASP.NET

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "the steps of rewriting the second-level domain name with URLRewriter under ASP.NET". In the daily operation, I believe that many people have doubts about the steps of rewriting the second-level domain name with URLRewriter under ASP.NET. The editor consulted all kinds of materials and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the doubts about "the steps of rewriting the second-level domain name with URLRewriter under ASP.NET". Next, please follow the editor to study!

Here, the domain name is required to be rewritten from http://1234.abc.com/ to ~ / Defa.aspx?id=1234.

*: domain name

First of all, the domain name should support universal interpretation, that is, the host name of the domain name is an asterisk *, for example: * .abc.com. The figure below is as follows

This ensures that you enter any prefixes in the browser's address bar and DNS will point them to the IP address you specify.

Second: IIS setting (Win2003 + IIS 6 as an example)

(1) the website must be the default site for the Web server, that is, the site with a port number of 80 and an empty host header. This is shown in the following figure.

The site receives all HTTP requests to the server (except for other sites with host headers). So any second-level domain name access to the server will be handled by the site.

(2) add ASP.NET 's Web request handler aspnet_isapi.dll to the wildcard application mapping list of the site. This is shown in the following figure.

The setting here is to leave all requests received by the site to aspnet_isapi.dll for processing.

Third: modify the URLRewriter of Microsoft.

Run the open source project URLRewriter. There are two areas that need to be modified:

(1) BaseModuleRewriter.cs class

Protected virtual void BaseModuleRewriter_AuthorizeRequest (object sender, EventArgs e) {HttpApplication app = (HttpApplication) sender; / / Rewrite (app.Request.Path, app); Rewrite (app.Request.Url.AbsoluteUri, app); / / # # modified here}

Here app.Request.Path is replaced with app.Request.Url.AbsoluteUri

(2) ModuleRewriter.cs class

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节点下添加如下一行代码 这里配置一个重写配置的类 (2)修改httpModules节点,在里面添加一行配置代码 (3)在主节点configuration节点下添加路由规则,代码如下: http://(\w+).abc.com/ ~/Defa.aspx?id=$1 代码里一个RewriterRule节点就是一个规则,这时只有一个,即把域名中的主机头部分做为Defa.aspx页面的id参数的值发送给Defa.aspx页面。 注意:这里LookFor节点里的http://(\w+).abc.com/不能少了***的反斜框 OK,一切完工 发布,上传到服务器,测试一下,如图

This test requires a class library: URLRewriter.dll (test version 1.0.1495.18710)

At this point, the study of "the steps of rewriting a second-level domain name with URLRewriter under ASP.NET" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report