从上一篇文章,我们可以实现对域名后面的那部分进行重写,那么可不可以对前面那部分进行重写而实现二级域名呢? 答案是肯定的。 这样,首先我们得修改UrlRewriter,怎么修改请参见江大鱼的BLog。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);
动态的添加如 xxx.xx.com这样的二级域名
》》这个需要通过域名商那里的服务去修改的吧
客户的域名a.com,之下所设的*.a.com全部解析到同一个IP地址上去。比如客户设b.a.com就会自已自动解析到与a.com同一个IP地址上去,显示的是跟a.com一样的页面。(以上是抄 来的)
你只要分析对方请求的URL也就是*.a.com的*部分,其他的就没什么了
如果结合地址重写就更完美了
Http://blyct.cnblogs.com
好了,开始讲述我的应用经验,这只是很菜鸟的经验,高手就不用看了。第一步,请从此下载此组件。解压,把UrlRewriter.dll copy到你的项目 bin 目录下。第二步,在Web.config中加入:<?xml version="1.0" encoding="gb2312" ?>
<configuration>
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>第二步,加入重写的规则节点:
如:
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/Sell/(.[0-9]*)\.html</LookFor>
<SendTo>~/Search/Search_Sell.aspx?id=$1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Sell/Search_Sell\.aspx</LookFor>
<SendTo>~/Search/Search_Sell.aspx</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Buy/(.[0-9]*)\.html</LookFor>
<SendTo>~/Search/Search_Buy.aspx?id=$1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Buys/(.[0-9]*)\.html</LookFor>
<SendTo>~/Buys/Show.aspx?id=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>这个就要根据你的需要了,如果你对正则表达式不熟,那么没办法,要么凭借你的高智商去找其中规律,稍稍改一下就能为你所用了。呵呵。如果实在搞不清,那就自己GOOGLE一下正则表达式吧。(本人开始是参考别人的配置猜的,竟然用对了,呵呵。后来还是看了一下相关资料,发现这东东很有用。)第三步,加入模块配置(写在<system.web>里面):
如:
<httpHandlers>
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>
(这里表示使用HTTP程序来处理重写)好了,到了现在我们可以试一下看。于是输入:http://127.0.0.1:8080/Sell/1.aspx 出现了,呵呵。但是如果所它改为:http://127.0.0.1:8080/Sell/1.html
晕,发现不行。汗
呵呵,原因是没把HTML的解析用 asp.net 的ISAPI来解析。
办法是第四步,在IIS\你的站点\属性\主目录\配置\映谢 加入一个和 aspx 页面的配置相同的扩展名项。注意“确认文件是否存在”不要勾选,否则会出现找不到文件。现在再来试试看。什么?#¥%#¥%#,还是不行。呵呵。不要急,咱们回过头再来看看,原来在 web.config 中我们没有配置 .html 也使用模块此解析。第五步,在模块配置中加入:
<httpHandlers>
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>现在总可以了吧,呵呵。终于看到了,兴奋吧。不要急,这还只是最简单的。如果你的页面有回传。比如说放了DATAGRID,有分页的,你点到下一页就发现,晕倒,又出问题了。
这下怎么办呢,这个其实微软件的网站上就有说到,我在这里简述一下了。
第六步,加入窗体回传保持的组件:
在原来你下载的项目里找到 ActionlessForm.dll 放到你的项目 bin 目录下。然后在你的这个页面中加入:
<%@ Register TagPrefix="skm" Namespace="ActionlessForm" Assembly="ActionlessForm" %>
再把你的<Form...>改为:
<skm:Form id="你的表单名" method="post" runat="server">
.....
</skm:Form>That's All.现在你可以高枕无忧了。一切如你所愿。最后,恭祝各位一切顺利。
答案是肯定的。
这样,首先我们得修改UrlRewriter,怎么修改请参见江大鱼的BLog。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目录下。 修改完了这后,我们再把此 UrlRewriter.dll COPY 到我们项目的Bin目录下。这样就结了么?没有。
首先请确定你的项目之前有按我上篇文章中写到的那样做过UrlRewriter的配置,否则请先回过头来看看那篇文章。如果你的项目已配置过,那么,我们还要为此做以下几件事情:1。请确定你的域名是支持泛解析的。然后你的网站为默认网站,否则将不能实现(至少我现在还没有找到好办法)
2。在IIS配置:在IIS\你的站点\属性\主目录\配置\映谢 在通配符应用程序配置处插入一个新的映谢。把可执行文件设为和上面ASPX页面同样的配置即可(注意不要勾选 “确定文件是否存在”)。(用处就是使所有请求通过 asp.net 的ISAPI来处理,只有这样才能对所有地址进行重写嘛。)
3。查看下你的网站主机头,里面的第一个主机头值必须为空,否则会出现错误的请求。后面就随你加了,看你想绑定多少域名了。(这个办法是江大鱼想出来的。为这个错误我们都想了好多办法。在这里感谢江大鱼)
4。最后改写你的 web.config 文件。
把上节中说到的
<httpHandlers>
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>
改为:
<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
</httpModules>
(就是改用HTTP 模块来执行重写,而不用HTTP 程序,否则无法重写地址前面。)
然后就来修改我们的重写正则了:
<RewriterRule>
<LookFor>http://(.[0-9]*)\.178b2b\.com/</LookFor>
<SendTo>~/Search/Search_Sell.aspx?id=$1</SendTo>
</RewriterRule>
好了,现在你输入 http://1.178b2b.com/ 就能搜索出相应分类了。但是聪明的你马上就发现。晕死,首页进不去了。呵呵。当然喽。你还得为首页加入重写正则。
<RewriterRule>
<LookFor>http://www\.178b2b\.com/</LookFor>
<SendTo>~/index.htm</SendTo>
</RewriterRule>大功告成。感觉爽死了吧。呵呵。莫急,如果你二级域名指向的目录下面的页面都用的相对地址连接的图片和其它页面的话,呵呵,你有得忙了,你要全部改成如下方式:
<a href=http://www.178b2b.com/cxlm/league.html target="_blank">诚信联盟</a>以上就是用UrlRewriter实现二级域名的方法了。希望各位一切顺利。最后,感谢江大鱼的无私帮助。