|
先把配置文件从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"));
///************************************************************************************
///
/// 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)
{
///**********************************************************************************
/// 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;
///************************************************************
/// Description:修补黑客可能采用".."的方法进入其他目录的问题
///************************************************************
string strURL = Server.UrlDecode(Request.RawUrl);
if (strURL.IndexOf("..") != -1)
{
throw new HttpException(404, "Not Found");
}
///**********************************************************************************
/// 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.LookFor + "$";
//string lookFor = "^" + Rewriter.ResolveUrl(app.Context.Request.ApplicationPath, rules.LookFor + requestedPath) + "$";
Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
if (re.IsMatch(host))
{
string sendToUrl = Rewriter.ResolveUrl(app.Context.Request.ApplicationPath, rules.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那里了。
6款热销日系车最高降3.1万
·奇瑞捷豹路虎组新机构 昌河铃木召回3万辆·自由光3.2L 5月将上市 起亚将搭7速双离合·奥迪全新TT巴黎首发 改款Polo GTI将搭6MT·新标致408七月上市 奔驰新车明年陆续推出·思域降1.5万 锐志优惠2万 朗动优惠1.1万·四款10万家用两厢车推荐 时尚更需大空间·改装奔驰SLK55 AMG 汽车车灯升级知识详解·霸气黄金车之翼 液态金属福特Torino
|
|