虚拟二级域名解决方案,值的看看[推荐]

很多时候,我们都想着该怎么去做二级域名的系统,其实,微软已经做出了类似的东西,基于.NET下的,我想大家应该都知道,就是URLRewriter这个东东

下载地址:http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi
原文地址:http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx

当然,它的这个做的比较简单,只是对地址后面的进行修改,是没办法做二级域名的。前几天在博客园里发现已经有高人把这个代码分析出来了。

方法一:.NET方式下

作者:江大鱼的BLOG ,Blog地址:http://jzywh.cnblogs.com/ 他里面有详细的关于修改UrlreWriting实现二级域名的方法,在这里我转下:

文章内容:
要实现这个功能,前提条件就是  www.abc.com 是泛解析的,再就是要修改一下URLRewriter了。
总共要修改2个文件

1.BaseModuleRewriter.cs

protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
        
{
            HttpApplication app 
= (HttpApplication) sender;
            Rewrite(app.Request.Path, app);
        }

改为

protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
        
{
            HttpApplication app 
= (HttpApplication) sender;
            Rewrite(app.Request.Url.AbsoluteUri, app);
        }


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

2.ModuleRewriter.cs

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

                
// 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
                }

            }

改为

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 = "^" + 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
                }

            }



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

改成了

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


完成这2处改动之后重新编译项目,将生成的dll复制到bin目录下。

再就是写web.config里的重写正则了

<RewriterRule>
            
<LookFor>http://(\d+)\.abc\.com</LookFor>
            
<SendTo>/show.aspx?id=$1</SendTo>
        
</RewriterRule>


好了大功告成,你在IE地址栏输入http://1234.abc.com,就可以看到http://www.abc.com/show.aspx?id=1234

的结果了

方法二:用ISAPI_Rewrite实现反向代理(ReverseProxy)
ISAPI_Rewrite是一个强大的基于正则表达式的URL处理引擎。它非常类似于Apache's mod_Rewrite,但它是专为IIS设计的。
ISAPI_Rewrite有两个版本:ISAPI_Rewrite Full与ISAPI_Rewrite Lite。
ISAPI_Rewrite Lite是免费版本,但不支持反向代理功能。
ISAPI_Rewrite Full只能下载到30天的试用版本。
这篇文章介绍的是基于ISAPI_Rewrite Full实现反向代理。配置实际上很简单。写出来的目的主要是希望给初次使用ISAPI_Rewrite的朋友提供参考。
下面就是配置步骤:
1、下载ISAPI_Rewrite Full: http://www.helicontech.com/download/#isapi_rewrite
2、安装ISAPI_Rewrite Full
3、修改配置文件httpd.ini,默认位置在C:\Program Files\Helicon\ISAPI_Rewrite。假如我们现在有两台Web服务器,一台是http://www.cnblogs.com/, 另一台是 www2.cnblogs.com,  www2作为一台反向代理服务器,客户端浏览器访问www2服务器,www2服务器向www服务器请求内容并返回给客户端。具体在httpd.ini的配置如下:
在httpd.ini中增加下面的内容:
RewriteCond Host: www2\.cnblogs\.com
RewriteProxy (.*) http\://www.cnblogs.com$1 [I,F,U]

参考配置文档:http://www.isapirewrite.com/docs/

二级域名配置:
RewriteCond Host: (?!\.|www|ww)(.*).yourname.com
RewriteRule (.*) http\://www.yourname.com/$1$2 [I,R]


其实,不管是哪中方式,无非是一个重定向的功能,以前在Asp里面这个是严重的缺陷,现在.NET可以做了,简单实现。
posted on 2006-06-20 08:33  北极熊,我来了!  阅读(728)  评论(0编辑  收藏  举报