高分求救,关于url重写,实现任意二级域名,绑定到二级目录的问题

aspxlove 2008-07-03 09:05:22
我看了网上url重写实现任意二级域名或多级域名的方法。
原文地址:url重写实现任意二级域名或多级域名

现在想实现例如:http://test.abc.com/ 到 http://www.abc.com/test/的重写
test目录下首页的,如Index.html或者default.html,或其它默认静态页面

上面其实就是http://test.abc.com/ 到 http://www.abc.com/test/Index.html的重写

要用重写HttpModule 实现了任意二级域名,是转到文件夹如test下面

高手们,怎么实现?
...全文
341 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
aspxlove 2008-08-03
  • 打赏
  • 举报
回复
再顶一下
aspxlove 2008-07-18
  • 打赏
  • 举报
回复
看来只能结帖了,顶者有分
aspxlove 2008-07-11
  • 打赏
  • 举报
回复
好的,再试一下
sheng9hhd 2008-07-10
  • 打赏
  • 举报
回复
正则改掉:(\w+)\.abc\.com
sheng9hhd 2008-07-10
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace Code.Web.HttpModule
{
public class URLRewriter : IHttpModule
{
#region IHttpModule 成员

public void Dispose()
{
throw new Exception("The method or operation is not implemented.");
}

public void Init(HttpApplication context)
{
try
{
// 取得原始URL屏蔽掉参数
string Url = context.Request.RawUrl;
// 建立正则表达式
Regex Reg = new Regex(@"/show-(\d+)-(\d+)\..+", RegexOptions.IgnoreCase);
// 用正则表达式进行匹配
Match m = Reg.Match(Url, Url.LastIndexOf("/"));
// 从最后一个“/”开始匹配
if (m.Success)
// 重写路径
context.RewritePath(@"~/aspx/show.aspx?type=" + m.Groups[1] + "&id=" + m.Groups[2]);
else
context.Response.Redirect(context.Request.Url.ToString());
}
catch
{
context.Response.Redirect(context.Request.Url.ToString());
}
}

#endregion
}
}
aspxlove 2008-07-10
  • 打赏
  • 举报
回复
嗯,还没调试成功,只能结帖了
aspxlove 2008-07-08
  • 打赏
  • 举报
回复
再顶一下
aspxlove 2008-07-06
  • 打赏
  • 举报
回复
不一定要用HttpModule 实现

只要能实现就行了,url重写任意二级域名
newlist010 2008-07-06
  • 打赏
  • 举报
回复
友情up
zhxhdean 2008-07-06
  • 打赏
  • 举报
回复
貌似有点道理!
aspxlove 2008-07-06
  • 打赏
  • 举报
回复
aspxlove 2008-07-05
  • 打赏
  • 举报
回复
最后顶一下
liying520 2008-07-05
  • 打赏
  • 举报
回复
<configSections>

<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />

</configSections>

<httpModules>

<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />

</httpModules>

<!-- 下面是配置重写URL规则 -->

<RewriterConfig>

<Rules>

<RewriterRule>

<LookFor>~/Products/Jurisdiction_(\w{3})\.aspx</LookFor>

<SendTo>~/En/Jurisdiction.aspx?jurid=$1</SendTo>

</RewriterRule>

<RewriterRule>

<LookFor>~/Articles/(\d{1,})\.aspx</LookFor> <!-- 这个是被代替后的文件名,使用到正则表达式 -->

<SendTo><![CDATA[~/En/Article_view.aspx?article_id=$1]]></SendTo> <!-- 这个是要给代替的网页,一般是带有问号后面带参数的网页 -->

</RewriterRule>

<RewriterRule>

<LookFor>~/Articles/(\d{1,})_(\d{1,})\.aspx</LookFor>

<SendTo><![CDATA[~/En/Article_view.aspx?article_id=$1&page=$2]]></SendTo>

</RewriterRule>

</Rules>

</RewriterConfig>


这样比如上面的网址http://localhost/En/Article_View.aspx?article_id=9就可以用http://localhost/Articles/9.aspx来代替,当然,你代替后的扩展名可以用任何iis能解释的扩展名,如果你喜欢用htm做扩展名,那么在配置转发规则上面配置为htm为扩展名的,同样有些文章可能很长,往往我们会把一个文章分成几页,那么根据上面的配置,我们如果想访问http://localhost/En/Article_View.aspx?article_id=9&page=3我们就可以用http://localhost/Articles/9_3.aspx来代替,这样当搜索引擎来抓起你的网页的时候,就会收录你这些网址下去,别人搜索到你网页的时候,就可以从这些地址链接过来。呵呵,是不是很方便呢?不需要修改任何程序,也不需要再占用多余的网络空间,轻松对付搜索引擎。
Feiin 2008-07-05
  • 打赏
  • 举报
回复
顶....
lingxyd_0 2008-07-05
  • 打赏
  • 举报
回复
不懂,帮楼主顶起来!!
itymx 2008-07-05
  • 打赏
  • 举报
回复
顶 学习
  • 打赏
  • 举报
回复
"要用重写HttpModule 实现了任意二级域名"--在HttpHander中重写岂不更好?
足球中国 2008-07-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 IcyPlayer 的回复:]

protected void Application_BeginRequest(object sender, EventArgs e)
{
string oldUrl = HttpContext.Current.Request.RawUrl;
string pattern = @"^(.+)newsclass/(\d+)\.aspx(\?.*)*$";
string replace = "$1newsclass.aspx?id=$2";
if (Regex.IsMatch(oldUrl, pattern, RegexOptions.IgnoreCase ¦ RegexOptions.Compiled))
{
string newUrl …
[/Quote]
aspxlove 2008-07-04
  • 打赏
  • 举报
回复
再顶一下
aspxlove 2008-07-03
  • 打赏
  • 举报
回复
再顶一下
加载更多回复(9)

62,046

社区成员

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

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

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

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