【转自】对于URL重写,支持无后缀url请求

 

有些资料讲如果要支持目录必须使用iiswriter,或者其他软件,其实通过简单对iis配置,再利用urlwriter就可以完美解决url重写的问题

可以将http://abc.domain.com/blog

转向到http://www.domain.com/xxx.aspx?username=abc

当然首先要将主机的泛域名支持打开。

做法是

A。打开IIS,右击站点(虚拟目录)-》属性-》主目录-》配置-》插入-》C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,不确认文件存在-》确定

B。修改webconfig:

  <RewriterConfig>
    <Rules>
      <RewriterRule>
        <LookFor>http://localhost/(blog)</LookFor>
        <SendTo>~/sdfsdf.asxp?tag=$1</SendTo>
      </RewriterRule>
   </Rules>
</RewriterConfig>

C。修改UrlRewriter
从微软下载源码,

1.BaseModuleRewriter.cs

【转】对于URL重写,支持无后缀url请求protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
【转】对于URL重写,支持无后缀url请求【转】对于URL重写,支持无后缀url请求        
【转】对于URL重写,支持无后缀url请求{
【转】对于URL重写,支持无后缀url请求            HttpApplication app 
= (HttpApplication) sender;
【转】对于URL重写,支持无后缀url请求            Rewrite(app.Request.Path, app);
【转】对于URL重写,支持无后缀url请求        }

改为

【转】对于URL重写,支持无后缀url请求protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
【转】对于URL重写,支持无后缀url请求【转】对于URL重写,支持无后缀url请求        
【转】对于URL重写,支持无后缀url请求{
【转】对于URL重写,支持无后缀url请求            HttpApplication app 
= (HttpApplication) sender;
【转】对于URL重写,支持无后缀url请求            Rewrite(app.Request.Url.AbsoluteUri, app);
【转】对于URL重写,支持无后缀url请求        }


就是将  app.Request.Path 替换成了  app.Request.Url.AbsoluteUri

2.ModuleRewriter.cs

【转】对于URL重写,支持无后缀url请求for(int i = 0; i < rules.Count; i++)
【转】对于URL重写,支持无后缀url请求【转】对于URL重写,支持无后缀url请求            
【转】对于URL重写,支持无后缀url请求{
【转】对于URL重写,支持无后缀url请求                
// get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
【转】对于URL重写,支持无后缀url请求
                string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
【转】对于URL重写,支持无后缀url请求
【转】对于URL重写,支持无后缀url请求                
// Create a regex (note that IgnoreCase is set【转】对于URL重写,支持无后缀url请求)
【转】对于URL重写,支持无后缀url请求
                Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
【转】对于URL重写,支持无后缀url请求
【转】对于URL重写,支持无后缀url请求                
// See if a match is found
【转】对于URL重写,支持无后缀url请求
                if (re.IsMatch(requestedPath))
【转】对于URL重写,支持无后缀url请求【转】对于URL重写,支持无后缀url请求                
【转】对于URL重写,支持无后缀url请求{
【转】对于URL重写,支持无后缀url请求                    
// match found - do any replacement needed
【转】对于URL重写,支持无后缀url请求
                    string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
【转】对于URL重写,支持无后缀url请求
【转】对于URL重写,支持无后缀url请求                    
// log rewriting information to the Trace object
【转】对于URL重写,支持无后缀url请求
                    app.Context.Trace.Write("ModuleRewriter""Rewriting URL to " + sendToUrl);
【转】对于URL重写,支持无后缀url请求
【转】对于URL重写,支持无后缀url请求                    
// Rewrite the URL
【转】对于URL重写,支持无后缀url请求
                    RewriterUtils.RewriteUrl(app.Context, sendToUrl);
【转】对于URL重写,支持无后缀url请求                    
break;        // exit the for loop
【转】对于URL重写,支持无后缀url请求
                }

【转】对于URL重写,支持无后缀url请求            }

改为

【转】对于URL重写,支持无后缀url请求for(int i = 0; i < rules.Count; i++)
【转】对于URL重写,支持无后缀url请求【转】对于URL重写,支持无后缀url请求            
【转】对于URL重写,支持无后缀url请求{
【转】对于URL重写,支持无后缀url请求                
// get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
【转】对于URL重写,支持无后缀url请求
                string lookFor = "^" + rules[i].LookFor + "$";
【转】对于URL重写,支持无后缀url请求
【转】对于URL重写,支持无后缀url请求                
// Create a regex (note that IgnoreCase is set【转】对于URL重写,支持无后缀url请求)
【转】对于URL重写,支持无后缀url请求
                Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
【转】对于URL重写,支持无后缀url请求
【转】对于URL重写,支持无后缀url请求                
// See if a match is found
【转】对于URL重写,支持无后缀url请求
                if (re.IsMatch(requestedPath))
【转】对于URL重写,支持无后缀url请求【转】对于URL重写,支持无后缀url请求                
【转】对于URL重写,支持无后缀url请求{
【转】对于URL重写,支持无后缀url请求                    
// match found - do any replacement needed
【转】对于URL重写,支持无后缀url请求
                    string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
【转】对于URL重写,支持无后缀url请求
【转】对于URL重写,支持无后缀url请求                    
// log rewriting information to the Trace object
【转】对于URL重写,支持无后缀url请求
                    app.Context.Trace.Write("ModuleRewriter""Rewriting URL to " + sendToUrl);
【转】对于URL重写,支持无后缀url请求
【转】对于URL重写,支持无后缀url请求                    
// Rewrite the URL
【转】对于URL重写,支持无后缀url请求
                    RewriterUtils.RewriteUrl(app.Context, sendToUrl);
【转】对于URL重写,支持无后缀url请求                    
break;        // exit the for loop
【转】对于URL重写,支持无后缀url请求
                }

【转】对于URL重写,支持无后缀url请求            }



string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";

改成了

string lookFor = "^" + rules[i].LookFor + "$";


OK。

 

相关文章:

  • 2021-06-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-22
  • 2021-10-10
  • 2022-01-16
  • 2022-12-23
  • 2021-06-23
猜你喜欢
  • 2022-02-22
  • 2021-10-25
  • 2022-12-23
  • 2021-08-25
  • 2021-06-30
  • 2021-10-15
相关资源
相似解决方案