对于URL重写,支持无后缀url请求【转】
原文出处:http://blog.csdn.net/jelink/archive/2006/10/03/1319869.aspx
有些资料讲如果要支持目录必须使用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
 protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e) 
 {
        { 
 HttpApplication app = (HttpApplication) sender;
            HttpApplication app = (HttpApplication) sender; 
 Rewrite(app.Request.Path, app);
            Rewrite(app.Request.Path, app); 
 }
        } 
改为
 protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e) 
 {
        { 
 HttpApplication app = (HttpApplication) sender;
            HttpApplication app = (HttpApplication) sender; 
 Rewrite(app.Request.Url.AbsoluteUri, app);
            Rewrite(app.Request.Url.AbsoluteUri, app); 
 }
        } 
就是将 app.Request.Path 替换成了 app.Request.Url.AbsoluteUri
2.ModuleRewriter.cs
 for(int i = 0; i < rules.Count; i++)
for(int i = 0; i < rules.Count; i++) 
 {
            { 
 // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
                // 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 = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$"; 
 
 
 // Create a regex (note that IgnoreCase is set
                // Create a regex (note that IgnoreCase is set )
) 
 Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
                Regex re = new Regex(lookFor, RegexOptions.IgnoreCase); 
 
 
 // See if a match is found
                // See if a match is found 
 if (re.IsMatch(requestedPath))
                if (re.IsMatch(requestedPath)) 
 {
                { 
 // match found - do any replacement needed
                    // match found - do any replacement needed 
 string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
                    string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo)); 
 
 
 // log rewriting information to the Trace object
                    // log rewriting information to the Trace object 
 app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
                    app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl); 
 
 
 // Rewrite the URL
                    // Rewrite the URL 
 RewriterUtils.RewriteUrl(app.Context, sendToUrl);
                    RewriterUtils.RewriteUrl(app.Context, sendToUrl); 
 break;        // exit the for loop
                    break;        // exit the for loop 
 }
                } 
 }
            } 
改为
 for(int i = 0; i < rules.Count; i++)
for(int i = 0; i < rules.Count; i++) 
 {
            { 
 // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
                // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory) 
 string lookFor = "^" + rules[i].LookFor + "$";
                string lookFor = "^" + rules[i].LookFor + "$"; 
 
 
 // Create a regex (note that IgnoreCase is set
                // Create a regex (note that IgnoreCase is set )
) 
 Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
                Regex re = new Regex(lookFor, RegexOptions.IgnoreCase); 
 
 
 // See if a match is found
                // See if a match is found 
 if (re.IsMatch(requestedPath))
                if (re.IsMatch(requestedPath)) 
 {
                { 
 // match found - do any replacement needed
                    // match found - do any replacement needed 
 string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
                    string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo)); 
 
 
 // log rewriting information to the Trace object
                    // log rewriting information to the Trace object 
 app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
                    app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl); 
 
 
 // Rewrite the URL
                    // Rewrite the URL 
 RewriterUtils.RewriteUrl(app.Context, sendToUrl);
                    RewriterUtils.RewriteUrl(app.Context, sendToUrl); 
 break;        // exit the for loop
                    break;        // exit the for loop 
 }
                } 
 }
            } 
将
string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
改成了
string lookFor = "^" + rules[i].LookFor + "$";
OK。
有些资料讲如果要支持目录必须使用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
 protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)  {
        {  HttpApplication app = (HttpApplication) sender;
            HttpApplication app = (HttpApplication) sender;  Rewrite(app.Request.Path, app);
            Rewrite(app.Request.Path, app);  }
        } 改为
 protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)  {
        {  HttpApplication app = (HttpApplication) sender;
            HttpApplication app = (HttpApplication) sender;  Rewrite(app.Request.Url.AbsoluteUri, app);
            Rewrite(app.Request.Url.AbsoluteUri, app);  }
        } 就是将 app.Request.Path 替换成了 app.Request.Url.AbsoluteUri
2.ModuleRewriter.cs
 for(int i = 0; i < rules.Count; i++)
for(int i = 0; i < rules.Count; i++)  {
            {  // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
                // 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 = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";  
  // Create a regex (note that IgnoreCase is set
                // Create a regex (note that IgnoreCase is set )
)  Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
                Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);  
  // See if a match is found
                // See if a match is found  if (re.IsMatch(requestedPath))
                if (re.IsMatch(requestedPath))  {
                {  // match found - do any replacement needed
                    // match found - do any replacement needed  string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
                    string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));  
  // log rewriting information to the Trace object
                    // log rewriting information to the Trace object  app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
                    app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);  
  // Rewrite the URL
                    // Rewrite the URL  RewriterUtils.RewriteUrl(app.Context, sendToUrl);
                    RewriterUtils.RewriteUrl(app.Context, sendToUrl);  break;        // exit the for loop
                    break;        // exit the for loop  }
                }  }
            } 改为
 for(int i = 0; i < rules.Count; i++)
for(int i = 0; i < rules.Count; i++)  {
            {  // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
                // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)  string lookFor = "^" + rules[i].LookFor + "$";
                string lookFor = "^" + rules[i].LookFor + "$";  
  // Create a regex (note that IgnoreCase is set
                // Create a regex (note that IgnoreCase is set )
)  Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
                Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);  
  // See if a match is found
                // See if a match is found  if (re.IsMatch(requestedPath))
                if (re.IsMatch(requestedPath))  {
                {  // match found - do any replacement needed
                    // match found - do any replacement needed  string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
                    string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));  
  // log rewriting information to the Trace object
                    // log rewriting information to the Trace object  app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
                    app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);  
  // Rewrite the URL
                    // Rewrite the URL  RewriterUtils.RewriteUrl(app.Context, sendToUrl);
                    RewriterUtils.RewriteUrl(app.Context, sendToUrl);  break;        // exit the for loop
                    break;        // exit the for loop  }
                }  }
            } 将
string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
改成了
string lookFor = "^" + rules[i].LookFor + "$";
OK。
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1319869
qq 42731209
 
                    
                     
                    
                 
                    
                 
 
         
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号