计算机理论

计算机网络
计算机理论
计算机应用
电子商务

本类阅读TOP10

·Frontpage网页制作
·C#做的ASP.NET登錄篇
·C语言实现串行通信接口程序
·Foxpro DBF数据库转换成SQL Server 6.5表的几种方法
·企业信息化的新选择——Intranet
·动态哈夫曼编码的改进
·论计算机网络中服务的概念
·Visual C++中的开放数据库连接技术
·关于烟汽应用计算机信息技术加强企业管理的调查报告
·Solaris下PRO*C和OCI程序设计分析与比较

分类导航
演讲致辞党团范文
心得体会领导讲话
经验介绍事迹材料
总结汇报计划方案
常用范文写作指南
证券金融银行管理
债务市场保险租赁
金融研究证券投资
财务管理投资决策
财务分析融资决策
财务管理市场营销
会计审计会计审计
成本会计管理会计
CPA行业管理学
战略竞争旅游管理学
成本管理管理学理论
物流管理人力资源管理
财政税收财政政策
财税法规税务研讨
税收理论国债研究
财政研究经济学
中国经济经济学理论
新经济学产业经济
国际经济经济学相关
地方经济发展战略
国际贸易公共管理
公共政策行政管理
经济管理企业战略
管理理论市场营销
企业研究企业文化
文化类西方文化
传统文化社会学相关
艺术学美学
音乐影视
艺术理论社会学
伦理道德环境保护
人口问题农村研究
教育学历史学
教育学国学
理工科理科相关
统计学物理学
工业设计交通
土建水利学材料工程学
电子学通信学
化工计算机
计算机网络计算机理论
计算机应用电子商务
文学外国语
人物研究哲学
哲学相关思想哲学
科技哲学中国哲学
西方哲学逻辑学
政治政治相关
民族主义资本主义
社会主义马克思主义
法律行政法
法学理论司法制度
经济法民法
医学医学
临床医学药学
其他文秘
公务员考试最新资讯
考试资料复习指导
面试指南教育教学
实现多个域名分别绑定到子目录

作者:未知 来源:应用文写作网 加入时间:2005-12-29 月光软件站

目前虚拟主机商提供将多个域名绑定到站点根目录,但是不提供类似cpanel那样可以将域名绑定到站点的子目录。
而当你手上有多个域名,网站空间和流量又有闲置的时候,是很希望
将这些资源利用起来,而且要做到降低做站的成本。而网络上流传的多域名绑到子目录多为判断HTTP_HOST再使用Asp的Response.Redirect或者php的header方法重订向到子目录去。这种方法在地址的请求上发生了变化,大家都知道Redirect的定向是很不友好的,在服务器端控制自动跳转会令访问者感到不安。
所以我需要的是对这个域名下面的所有请求都转接到对应的子目录里去
比如
http://www.xaradio.com/default.aspx
实际访问的是
http://www.3pub.com/xaradio/default.aspx
http://www.xaradio.com/album.aspx?id=722
实际访问的是
http://www.3pub.com/xaradio/album.aspx?id=722
http://www.xaradio.com/*.aspx
实际要访问到http://www.3pub.com/xaradio/*.aspx
而绑定到该站点根目录的其他域名和地址仍然不受影响
如:
http://www.3pub.com/

http://3pub.com/

http://www.3pub.com/default.aspx

http://3pub.com/default.aspx
http://www.aspxboy.com/484/default.aspx
该文章详细的描述了在Asp.Net中使用HttpModule和HttpHander来重写Url,读懂他特别是
http://www.aspxboy.com/484/archive.aspx#ekaa
节将是我们下面工作的前提朋友们可以下载该文章附带的代码研究。
如果您对HttpModule的编成非常熟悉那么可以向下进行了
一。 先把配置文件从web.config内移出为了不让web.config变的非常臃肿,我们将配置文件从web.config内移出
假设我们的多域名绑定配置文件为“MulitDomain.config“
将RewriterConfiguration.cs的public static RewriterConfiguration GetConfig()方法
修改如下:

/// 从XML配置文件中返回重写信息
///
/// RewriterConfiguration
public static RewriterConfiguration GetConfig()
{
RewriterConfiguration config = (RewriterConfiguration) BoovooCache.Get(CacheKey);
if(config == null)
{
// 2005-08-18 wu meibo update the config file to SiteUrls.config
// HttpContext.Current.Cache.Insert("RewriterConfig", ConfigurationSettings.GetConfig("RewriterConfig"));
///************************************************************************************
/// 
///  Author:活靶子,huobazi
/// Date:2005-08-18
///
///  Description:将配置文件移到单独的文件内,更新以下代码,原代码(上一行)停止工作
///
///************************************************************************************
string filePath = String.Empty;
if(HttpContext.Current != null)
{
filePath = HttpContext.Current.Server.MapPath("~/MulitDomain.config");
}
else
{
filePath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "MulitDomain.config";
}
XmlSerializer ser = new XmlSerializer(typeof(RewriterConfiguration));
FileStream fileReader = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
StreamReader reader = new StreamReader(fileReader);
config = (ser.Deserialize(reader)) as RewriterConfiguration;
reader.Close();
fileReader.Close();
if (File.Exists(filePath))
{
CacheDependency dep = new CacheDependency(filePath);
BoovooCache.Max(CacheKey,config,dep);
BoovooCache.ReSetFactor(config.CacheFactor);
}
}
return config;
}

二。做一些修补
RewriterModule.cs内


public virtual void Init(HttpApplication app)
{
///**********************************************************************************
///  Author:活靶子,huobazi
/// Date:2005-08-18
///  Description:增加BeginRequest,在内增加防止黑客可能利用的某些Url漏洞攻击的代码
///**********************************************************************************
app.BeginRequest += new EventHandler(this.RewriterModule_BeginRequest);
// 警告!此代码不适用于 Windows 身份验证!
// 如果使用 Windows 身份验证,
// 请改为 app.BeginRequest
app.AuthorizeRequest += new EventHandler(this.RewriterModule_AuthorizeRequest);
} protected virtual void RewriterModule_BeginRequest(object o , EventArgs e)
{
HttpApplication app = ((HttpApplication)(o));
HttpServerUtility Server = app.Server;
HttpRequest Request = app.Request;
///************************************************************
///  Author:活靶子,huobazi
/// Date:2005-08-18
/// Description:修补黑客可能采用".."的方法进入其他目录的问题
///************************************************************
string strURL = Server.UrlDecode(Request.RawUrl);
if (strURL.IndexOf("..") != -1)
{
throw new HttpException(404, "Not Found");
}
///**********************************************************************************
///  Author:活靶子,huobazi
/// Date:2005-08-18
/// Description:修补"规范化"问题 see:
http://support.microsoft.com/?kbid=887459
///***********************************************************************************
if (Request.Path.IndexOf('\\') >= 0 ||
Path.GetFullPath(Request.PhysicalPath) != Request.PhysicalPath)
{
throw new HttpException(404, "Not Found");
}
}

三。开始匹配域名


protected void Rewrite(string requestedPath, System.Web.HttpApplication app)
{
string host = app.Context.Request.Url.Host.ToString().ToLower();

app.Context.Trace.Write("RewriterModule", "Entering ModuleRewriter");  
RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;
for(int i = 0; i < rules.Count; i++)
{//将MulitDomain.config内定义的规则LookFor的值逐个匹配当前主机名判断否被定义了需要重写
//如果匹配则需要重写,那将请求重写到SendTo定义的目录内的该文件
string lookFor = "^" + rules[i].LookFor + "$";
//string lookFor = "^" + Rewriter.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor + requestedPath) + "$";
Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
if (re.IsMatch(host))
{
string sendToUrl = Rewriter.ResolveUrl(app.Context.Request.ApplicationPath,  rules[i].SendTo + requestedPath);
app.Context.Trace.Write("RewriterModule", "Rewriting URL to " + sendToUrl);
Rewriter.RewriteUrl(app.Context, sendToUrl);
break;
}
}
app.Context.Trace.Write("RewriterModule", "Exiting ModuleRewriter");
}

四。写规则文件
MulitDomain.config的匹配规则如下:


<?xml version="1.0" encoding="utf-8" ?>
<RewriterConfig>
<Rules>
  <RewriterRule>
   <LookFor>www\.xaradio\.com</LookFor>
   <SendTo>~/xaradio</SendTo>
  </RewriterRule>
  <RewriterRule>
   <LookFor>xaradio\.com</LookFor>
   <SendTo>~/xaradio</SendTo>
  </RewriterRule>
</Rules>
  </RewriterConfig>

 
最后说明一下,根目录下一定要有一个Default.aspx如果你的所有域名都按照这种方式“绑定”那么根目录下放置一个空Default.aspx就可以,该文件来“欺骗IIS” ,防止直接使用域名访问的时候IIS查找不到default或者index文件就报404错误,等到该检查过去之后权利已经移交到aspnet_isapi.dll那里了。




相关文章

相关软件