ASP.NET中实现二级或多级域名(修改UrlRewrite)
来源:互联网 发布:app上的数据接口 编辑:程序博客网 时间:2024/06/07 01:47
大家应该知道,微软的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
改为
就是将 app.Request.Path 替换成了 app.Request.Url.AbsoluteUri
<SCRIPT type=text/javascript><!--google_ad_client = "pub-4654382453554667";/* 468x60, 创建于 08-4-20 */google_ad_slot = "9836485125";google_ad_width = 468;google_ad_height = 60;//--></SCRIPT><SCRIPT src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript></SCRIPT><SCRIPT> window.google_render_ad(); </SCRIPT><IFRAME name=google_ads_frame marginWidth=0 marginHeight=0 src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4654382453554667&dt=1226116305886&lmt=1226116305&output=html&slotname=9836485125&correlator=1226116301900&url=http%3A%2F%2Fwww.cnblogs.com%2Fjzywh%2Farchive%2F2008%2F10%2F11%2F246650.html%231337974&ea=0&ref=http%3A%2F%2Fwww.cnblogs.com%2Fjzywh%2Farchive%2F2008%2F11%2F02%2FShareSession.html&frm=0&ga_vid=134687496.1226116302&ga_sid=1226116302&ga_hid=1055005667&flash=10.0.12.36&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_his=1&u_java=true" frameBorder=0 width=468 scrolling=no height=60 allowTransparency></IFRAME>
2.ModuleRewriter.cs
改为
将
string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
改成了
string lookFor = "^" + rules[i].LookFor + "$";
完成这2处改动之后重新编译项目,将生成的dll复制到bin目录下。
再就是写web.config里的重写正则了
好了大功告成,你在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<SCRIPT type=text/javascript><!--google_ad_client = "pub-4654382453554667";google_ad_width = 728;google_ad_height = 90;google_ad_format = "728x90_as";google_ad_type = "text";google_ad_channel ="";//--></SCRIPT><SCRIPT src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript></SCRIPT><SCRIPT src="http://googleads.g.doubleclick.net/pagead/test_domain.js"></SCRIPT><SCRIPT> window.google_render_ad(); </SCRIPT>
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);
}
{
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);
}
{
HttpApplication app = (HttpApplication) sender;
Rewrite(app.Request.Url.AbsoluteUri, app);
}
就是将 app.Request.Path 替换成了 app.Request.Url.AbsoluteUri
<SCRIPT type=text/javascript><!--google_ad_client = "pub-4654382453554667";/* 468x60, 创建于 08-4-20 */google_ad_slot = "9836485125";google_ad_width = 468;google_ad_height = 60;//--></SCRIPT><SCRIPT src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript></SCRIPT><SCRIPT> window.google_render_ad(); </SCRIPT><IFRAME name=google_ads_frame marginWidth=0 marginHeight=0 src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4654382453554667&dt=1226116305886&lmt=1226116305&output=html&slotname=9836485125&correlator=1226116301900&url=http%3A%2F%2Fwww.cnblogs.com%2Fjzywh%2Farchive%2F2008%2F10%2F11%2F246650.html%231337974&ea=0&ref=http%3A%2F%2Fwww.cnblogs.com%2Fjzywh%2Farchive%2F2008%2F11%2F02%2FShareSession.html&frm=0&ga_vid=134687496.1226116302&ga_sid=1226116302&ga_hid=1055005667&flash=10.0.12.36&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_his=1&u_java=true" frameBorder=0 width=468 scrolling=no height=60 allowTransparency></IFRAME>
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
}
}
{
// 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
}
}
{
// 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>
<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<SCRIPT type=text/javascript><!--google_ad_client = "pub-4654382453554667";google_ad_width = 728;google_ad_height = 90;google_ad_format = "728x90_as";google_ad_type = "text";google_ad_channel ="";//--></SCRIPT><SCRIPT src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript></SCRIPT><SCRIPT src="http://googleads.g.doubleclick.net/pagead/test_domain.js"></SCRIPT><SCRIPT> window.google_render_ad(); </SCRIPT>
=======================================================
另外一种方法:
.net动态创建二级域名URL重写
在 ASP.NET 中执行 URL 重写
http://msdn2.microsoft.com/zh-cn/library/ms972974.aspx
简单思路~~
注意,要保证你的域名泛解析,并且在主机上绑定主域名,如:绑定我的域名为keda100.com。
原理很简单,用泛解析后,test000.keda100.com(或者其它的子域名)都指向了我的主机ip,然后在主机默认处理页面里分析地址进行转向。(如我是在default.aspx页面里处理的)
代码实现:
我这里用的是.net2.0(c#)。
getUrl = Request.Url.AbsoluteUri.ToLower(); //获取URL(包含后面的网页) //http://www.keda100.com/index.aspx
//判断URL是否为空
if(getUrl != "")
{
string at = getUrl.Replace("http://", ""); //将URL中的"http://"去掉 www.keda100.com/index.aspx
userUrl = at;//www.keda100.com/index.aspx
userUrl = userUrl.Replace("www.", ""); //将URL中的"www."去掉 keda100.com/index.aspx
userUrl = userUrl.Split('.')[0]; //取字符串第一个点之前的部分 cn-web
uurl = at.Split('/')[0];//读取站点信息,www.keda100.com
string mosite = GetWebConfig.getDomain.Replace("http://*.","").Replace("/","");//keda100.com,//这里是从配置文件里获取信息(下面会提到部分配置文件代码)
if(at.IndexOf(GetWebConfig.SiteIP)!=-1)//这里是从我的配置文件里获取信息(下面会提到部分配置文件代码)
{
this.Response.Redirect("user/in.aspx");//如果是IP信息,直接用ip登陆
}
else//用的是域名登陆
{
if(at.IndexOf("www.")!=-1 || at.IndexOf(mosite)==-1)//处理一级域名
{
//处理用户站点信息
if(new BLL.ALD_MSite().Exists(uurl))
{
//这里代码省去,根据你的实际情况由域名信息获取用户信息进行转向
}
else
{
//一级域名不存在的处理!
}
}
}
web.config页面:
<add key="Site" value="http://*.keda100.com/" />
<add key="SiteIP" value="212.12.23.2" />
以上代码只是截取了核心实现代码,已经能全面说明问题了。
http://msdn2.microsoft.com/zh-cn/library/ms972974.aspx
简单思路~~
注意,要保证你的域名泛解析,并且在主机上绑定主域名,如:绑定我的域名为keda100.com。
原理很简单,用泛解析后,test000.keda100.com(或者其它的子域名)都指向了我的主机ip,然后在主机默认处理页面里分析地址进行转向。(如我是在default.aspx页面里处理的)
代码实现:
我这里用的是.net2.0(c#)。
getUrl = Request.Url.AbsoluteUri.ToLower(); //获取URL(包含后面的网页) //http://www.keda100.com/index.aspx
//判断URL是否为空
if(getUrl != "")
{
string at = getUrl.Replace("http://", ""); //将URL中的"http://"去掉 www.keda100.com/index.aspx
userUrl = at;//www.keda100.com/index.aspx
userUrl = userUrl.Replace("www.", ""); //将URL中的"www."去掉 keda100.com/index.aspx
userUrl = userUrl.Split('.')[0]; //取字符串第一个点之前的部分 cn-web
uurl = at.Split('/')[0];//读取站点信息,www.keda100.com
string mosite = GetWebConfig.getDomain.Replace("http://*.","").Replace("/","");//keda100.com,//这里是从配置文件里获取信息(下面会提到部分配置文件代码)
if(at.IndexOf(GetWebConfig.SiteIP)!=-1)//这里是从我的配置文件里获取信息(下面会提到部分配置文件代码)
{
this.Response.Redirect("user/in.aspx");//如果是IP信息,直接用ip登陆
}
else//用的是域名登陆
{
if(at.IndexOf("www.")!=-1 || at.IndexOf(mosite)==-1)//处理一级域名
{
//处理用户站点信息
if(new BLL.ALD_MSite().Exists(uurl))
{
//这里代码省去,根据你的实际情况由域名信息获取用户信息进行转向
}
else
{
//一级域名不存在的处理!
}
}
}
web.config页面:
<add key="Site" value="http://*.keda100.com/" />
<add key="SiteIP" value="212.12.23.2" />
以上代码只是截取了核心实现代码,已经能全面说明问题了。
- ASP.NET中实现二级或多级域名(修改UrlRewrite)
- ASP.NET中实现二级或多级域名(修改UrlRewrite)
- ASP.NET中实现二级或多级域名(修改UrlRewrite)
- ASP.NET中实现二级或多级域名(修改UrlRewrite)
- ASP.NET中实现二级或多级域名(修改UrlRewrite)
- ASP.NET中实现二级或多级域名(修改UrlRewrite)
- ASP.NET中实现二级或多级域名(修改UrlRewrite)
- ASP.NET中实现二级或多级域名(修改UrlRewrite)
- ASP.NET中实现二级或多级域名(修改UrlRewrite)需要注意的问题
- [转]ASP.NET中实现二级或多级域名(修改UrlRewrite)
- ASP.NET中实现二级或多级域名(修改UrlRewrite)需要注意的问题
- ASP.NET中实现二级或多级域名(修改UrlRewrite)需要注意的问题
- Asp.Net中实现二级或多级域名(URL重写)
- ASP.NET中实现二级或多级域名
- UrlRewrite实现二级或多级域名
- 修改UrlRewrite以对域名进行重写,即实现二级或多级域名
- 修改UrlRewrite以对域名进行重写,即实现二级或多级域名
- 修改UrlRewrite以对域名进行重写,即实现二级或多级域名
- .net学习推荐网站
- 《Java2入门经典JDK5》学习笔记——读文件(二)
- delphi 关闭线程函数
- 什么是CSS hack及写法。
- 关于Google恶意软件警告:“该网站可能含有恶意软件,有可能会危害您的电脑”
- ASP.NET中实现二级或多级域名(修改UrlRewrite)
- Java代码编写的30条建议
- Ruby中文社区的开源项目平台已经成功搭建起来了
- 创业团队第四次聚会
- ++项目会议【7.29】
- Visual Studio 2005 中的新的 DataSet 功能
- Windows API一日一练(18)EndDialog函数
- 液晶电视工作原理(转)
- 应用.Net 2.0新的FTP类创建相片查看程序