用URLRewriter实现URL和域名重写

    技术2022-05-11  8

    http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi 首先下载这个安装,把URLRewriter.dll复制到web的bin目录下面。 然后在web.config里面进行如下配置

    <configSections> <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" /> </configSections> <RewriterConfig> <Rules> <!-- 产品制表者规则 --> <RewriterRule> <LookFor>~/Products/Beverages/.aspx</LookFor> <SendTo>~/functions/displaypro.aspx?mid=1</SendTo> </RewriterRule> </Rules> </RewriterConfig> <httpHandlers> <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" /> </httpHandlers> 如果是IIS6.需要对网站做以下配置 站点属性-》主目录-》配置-》添加 C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/aspnet_isapi.dll .html 取消“确认文件是否存在”前的勾 如果是II7。需对网站做以下配置 网站的处理程序映射里面的对应.aspx的映射设置访问限制,作用设置为未指定.

     

    域名重写。

    首先要把网站域名泛解析到你本机。

    然后对URLRewriter的源代码里面的RewriterFactoryHandler.cs进行修改。

    public class RewriterFactoryHandler : IHttpHandlerFactory { /// <summary> /// GetHandler is executed by the ASP.NET pipeline after the associated HttpModules have run. The job of /// GetHandler is to return an instance of an HttpHandler that can process the page. /// </summary> /// <param name="context">The HttpContext for this request.</param> /// <param name="requestType">The HTTP data transfer method (<b>GET</b> or <b>POST</b>)</param> /// <param name="url">The RawUrl of the requested resource.</param> /// <param name="pathTranslated">The physical path to the requested resource.</param> /// <returns>An instance that implements IHttpHandler; specifically, an HttpHandler instance returned /// by the <b>PageParser</b> class, which is the same class that the default ASP.NET PageHandlerFactory delegates /// to.</returns> public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated) { // log info to the Trace object... context.Trace.Write("RewriterFactoryHandler", "Entering RewriterFactoryHandler"); //string sendToUrl = url;url重写 string sendToUrl = context.Request.Url.AbsolutePath;//域名重写 string filePath = pathTranslated; // get the configuration rules RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules; // iterate through the rules for (int i = 0; i < rules.Count; i++) { // Get the pattern to look for (and resolve its URL) string lookFor = "^" + RewriterUtils.ResolveUrl(context.Request.ApplicationPath, rules.LookFor) + "$"; // Create a regular expression object that ignores case... Regex re = new Regex(lookFor, RegexOptions.IgnoreCase); // Check to see if we've found a match //if (re.IsMatch(url))//URL重写 if (re.IsMatch(context.Request.Url.AbsolutePath))//域名重写 { // do any replacement needed //sendToUrl = RewriterUtils.ResolveUrl(context.Request.ApplicationPath, re.Replace(url, rules.SendTo));//url重写 sendToUrl = RewriterUtils.ResolveUrl(context.Request.ApplicationPath, re.Replace(context.Request.Url.AbsolutePath, rules.SendTo));//域名重写 // log info to the Trace object... context.Trace.Write("RewriterFactoryHandler", "Found match, rewriting to " + sendToUrl); // Rewrite the path, getting the querystring-less url and the physical file path string sendToUrlLessQString; RewriterUtils.RewriteUrl(context, sendToUrl, out sendToUrlLessQString, out filePath); // return a compiled version of the page context.Trace.Write("RewriterFactoryHandler", "Exiting RewriterFactoryHandler"); // log info to the Trace object... return PageParser.GetCompiledPageInstance(sendToUrlLessQString, filePath, context); } }


    最新回复(0)