zoukankan      html  css  js  c++  java
  • 用asp.net 2.0实现网站二级域名

    基本思路:
    1. 域名支持泛解析,即是指:把A记录 *.devin.cn 解析到服务器IP,服务器IIS中做绑定,绑定时主机头为空;
    2. 为了实现完全的二级域,建两个站点,一个为主站用,一个为用户用,两个站点目录都指到一个同一网站目录
    3. 在Web程序中或取URL来源中的二级域名主机头,比如:abc.devin.cn 中的 abc;
    4. 用获取来的二级域名名称,存入Session,方便取用
    5. 用获取来的二级域名名称,用URL重写地址

    实现方法:
    域名A记录解析不用说了吧 ^_^,就是做一个*.doitease.cn的A记录解析到你服务器IP

    方法一:二级域名URL转向
    a. 建立站点,在IIS中绑定域名(win2003 - IIS6)
    打开IIS,右击站点,然后属性,点击网站项IP地址的高级按钮,然后点编辑或添加来新增绑定,主机头值为空,如下图所示:

    这里只要绑定一个就行了,绑定后如下图所示:

    将此站点默认访问文档设为Index.aspx,。

    做好后,不管用什么样的二级前缀都会访问到这个网站。

    下面来获取URL地址进行分析,下面是过程代码,用来获取及判断主机头,代码放在默认文档的首文件Index.aspx.cs中

    ///


    ///取得二级域主机头值,并实行转向
    ///
    public void CheckDomain()
    {
    HostName = HttpContext.Current.Request.Url.Host.ToString(); //获取URL主机地址
    UserHost = HostName.Split(new Char[] { '.' }); //数组,以“.”分隔

    //判断二级域名地址是否符合abc.域名.com 这种格式,及数组UserHost长度不大于3,否则就跳传到其它页
    if (UserHost.Length > 3)
    {
    HttpContext.Current.Response.Redirect("http://www.doitease.cn/Error.aspx");//跳转到出错页面
    return;
    }

    UserDomainName = UserHost[0].ToString(); //取得数组最前面一组值,及二级域名主机头

    //进行特定判断,及是不要用到做为二级域名的主机头
    if (UserDomainName.ToLower() == "www" || UserDomainName.ToLower() == "doitease" || UserDomainName == null || UserDomainName.ToString()=="")
    {
    //你的动作
    }
    else {
    HttpContext.Current.Response.Redirect("/User/");//跳转到用户目录,即是二级域名所要去到的目录,当然你也可以跳传至*.aspx?UserID=xxx这样的链接
    return;
    }

    }

    到这里已经可以实现abc.devin.cn跳转到指定的页面或链接了,但并不是真正的二级域名,只是URL转向而已。


    方法二:真正的二级域名
    a.建立站点
    此时我们需要建立两个站点,一个主站点、一个二级域站点,二个站点的文件目录为同一目录,其目录中有Default.aspx及Index.aspx两个文件。建立方法如下:
    a). 主站点建立方法参照方法一中的站点建立,不过,主机头非空,需设为http://www.doitease.cn/ 与 doitease.cn ,当然,你也可以设置其它不想用来做二级域的主机头。站点默认访问文档为:Default.aspx。
    b). 二级域站点建立方法与方法一中的站点建立一样,其默认访问文档为:Index.aspx。

    b.现在我们要用到之前建立的二级域站点,我们将用户的数据放在根目录下的User目录中。
    下面是/User目录中默认文档(Index.aspx.cs)的过程代码,主要用途是将二级域名主机头存入Session,方便调用,也是二级域(用户区)的首页文件

    ///


    ///取得二级域主机头值,存入Session["UserDomainName"]中
    ///
    public void UserDomainNameSession()
    {
    HostName = HttpContext.Current.Request.Url.Host.ToString();//获取URL主机地址
    UserHost = HostName.Split(new Char[] { '.' });//数组,以“.”分隔

    //判断二级域名地址是否符合abc.域名.com 这种格式,及数组UserHost长度不大于3,否则就跳传到其它页
    if (UserHost.Length > 3)
    {
    HttpContext.Current.Response.Redirect("http://www.devin.cn//Error.aspx");//跳转到出错页面
    return;
    }

    UserDomainName = UserHost[0].ToString();//取得数组最前面一组值,及二级域名主机头

    //进行特定判断,及是不要用到做为二级域名的主机头
    if (UserDomainName.ToLower() == "www" || UserDomainName.ToLower() == "doitease" || UserDomainName == null || UserDomainName.ToString()=="")
    {
    //你的动作
    }
    else
    {
    HttpContext.Current.Session["UserDomainName"] = UserDomainName; //将二级域名主机头存入Session
    }
    }
    //你对Session["UserDomainName"]的处理,比如把这个Session["UserDomainName"]的值是“abc”,那么你可以index.aspx?UserName=abc,如果不想使用Session,你可以在需要用到二级域主机头时,再来通过URL地址获取。

    c.URL重写
    微软的URLRewrite能够对URL进行重写,但是也只能对域名之后的部分进行重写,而不能对域名进行重写,如:可将 http://http://www.abc.com//1234/  重写为 http://www.abc.com/show.aspx?id=1234  但不能将
    http://1234.abc.com/  重写为  http://www.abc.com/show.aspx?id=1234

    要实现这个功能,前提条件就是  http://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

    的结果了
    若你在实际应用中碰到了问题,请查看文章 "修改UrlRewrite以对域名进行重写"需要注意的问题 ,希望能够帮助你!

    附:
    URLRewriter 的相关资料
    http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx

  • 相关阅读:
    怎么样实现打印网页中指定的表格,而不是全页
    加深C# 中字符串前加@符号理解以及使用~~
    CommandArgument 绑定多个参数
    gridview等控件CommandField英文的解决.
    正式发布基于VS2008的AJAX模板包
    给datalist加自动编号
    .net生成文字图片
    重新注册.net
    Android JNI入门第二篇——Java参数类型与本地参数类型对照
    Android推送方式比较
  • 原文地址:https://www.cnblogs.com/SALIN/p/1005980.html
Copyright © 2011-2022 走看看