我看了网上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下面高手们,怎么实现?
原文地址: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下面高手们,怎么实现?
http://www.cnblogs.com/jzywh/archive/2006/02/20/seconddomainurlrewriter.html
http://www.cnblogs.com/jzywh/archive/2007/09/23/902905.html
http://www.cnblogs.com/jzywh/archive/2007/12/20/urlrewriteaction.html
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 = Regex.Replace(oldUrl, pattern, replace, RegexOptions.Compiled | RegexOptions.IgnoreCase);
this.Context.RewritePath(newUrl);
}
}
其实很简单,这是我从我程序里分离出来的,这段代码就可以把/newsclass/1234转换成/newsclass.aspx?id=1234,你改改用吧
这样比如上面的网址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来代替,这样当搜索引擎来抓起你的网页的时候,就会收录你这些网址下去,别人搜索到你网页的时候,就可以从这些地址链接过来。呵呵,是不是很方便呢?不需要修改任何程序,也不需要再占用多余的网络空间,轻松对付搜索引擎。
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
}
}