怎樣動態創建二級域名、子域名?

第一步:把域名設置成泛解析.
 就是把*.域名 解析到你的主機,是否支持泛解析,請查看你的域名商說明.
第二步:IIS設成的時候不要綁定域名.注意一臺服務器裏只能有一個站點不綁定域名
就是*.域名的默認訪問頁.
第三步:默認頁里加入以下代碼:關鍵部分如下:
string sURL=context.Request.ServerVariables["HTTP_HOST"].ToLower();
 sURL就是獲取的域名部分 .
  xxx.域名
  對應的xxx就是用戶名.然後查庫裏的記錄,如xxx對應的是www.csd.net那就就把
它轉向到www.csdn.net 或者寫一個框架頁隱藏直實域名

 

  第三步也可以寫成HttpHandler或者HttpModule模塊來處理
 HttpHandler模塊:
public class DnsHttpHandler: IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
            string sUSER;
            UserDns user;
string userDomain=string.Empty;
string sURL=context.Request.ServerVariables["HTTP_HOST"].ToLower();
if(sURL.IndexOf("@")==-1&&sURL.IndexOf(".")==-1)
{
//判段域名,轉向默認網址
context.Response.Redirect(DnsConfiguration.GetConfig().DefaultURL,true);
}

#region myhome設置
if(sURL=="保留域名")
{
context.Response.Redirect(DnsConfiguration.GetConfig().DefaultURL,true);
}
#endregion
#region 判段並獲取用戶名
sUSER=Utility.DomainToUser(sURL);
userDomain=Utility.GetFirstDomain(sURL);
//context.Response.Write(userDomain);
//context.Response.End();
user  =Users.GetUserDns(sUSER,userDomain);

if(user==null)
{
context.Response.Redirect(DnsConfiguration.GetConfig().DefaultURL,true);
}
          
         context.Response.Write( "<meta http-equiv=refresh content=/"0;url="+user.RedirectURL.Trim()+"/">");


#endregion

 

 

}

public bool IsReusable
{
get
{
return false;
}
}
}

 提取用戶名模塊:
public class Utility
{
public static string DomainToUser(string domain)
{
string username=string.Empty;
string[] first_name;
#region 判段並獲取用戶名

if(domain.IndexOf("@")==-1&&domain.IndexOf(".")==-1)
{
 return username;
}
if(domain.IndexOf(".")!=-1)
{
first_name=domain.Split( . );
if(first_name[0]=="www")
{
username=first_name[1];
}
else
{
username=first_name[0];
}
}
if(domain.IndexOf("@")!=-1)
{
first_name=domain.Split( @ );
if(first_name[0]=="www")
{
username=first_name[1];
}
else
{
username=first_name[0];
}
}
#endregion

 return username;
}

public static string GetFirstDomain(string domain)
{
 string[] first_name;
domain=domain.Replace( @ , . );
string temp=string.Empty;
if(domain.StartsWith("www."))
{
domain=domain.Replace("www.","");
}
if(domain.IndexOf( . )!=-1)
{
first_name=domain.Split( . );
}else
return string.Empty;
if(first_name.Length==2)
{
//sb.Append("{0}.{1}",first_name[0])
return String.Format("{0}.{1}",first_name[0],first_name[1]);
}
for(int i=1; i<first_name.Length;i++)
{
temp += first_name[i] + ".";
}
            temp=temp.Remove(temp.Length-1,1);
        return temp;
         
  
}
}

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章