isapi rewrite 设置问题

pccc1984 2007-12-20 09:16:42
比如我要将 blog.aa.com/aa 转到 www.aa.com/blog/index.aspx?id=aa 上要怎么做?
...全文
196 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
slimboy123 2007-12-20
  • 打赏
  • 举报
回复
用UrlReWriter可以实现,不过要修改下它的源代码

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目录下。
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://blog\.aa\.com/(.*) </LookFor>
<SendTo> ~/blog/index.aspx?id=$1 </SendTo>
</RewriterRule>


如果你的二级域名也要变动
<RewriterRule>
<LookFor> http://(.*)\.aa\.com/(.*) </LookFor>
<SendTo> ~/$1/index.aspx?id=$2 </SendTo>
</RewriterRule>

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧