我看了网上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下面高手们,怎么实现?

解决方案 »

  1.   

    http://www.svnhost.cn/Download/?k=urlrewrite
      

  2.   

    http://www.cnblogs.com/jzywh/archive/2005/09/29/246650.html
    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
      

  3.   


        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,你改改用吧
      

  4.   

    http://topic.csdn.net/u/20080701/14/ef8a1e85-a336-4aaf-9860-db864e01aa73.html
      

  5.   

    http://huobazi.cnblogs.com/archive/2005/10/15/SubdomainsWithHttpModuleInAspDotNet.html
      

  6.   

    "要用重写HttpModule 实现了任意二级域名"--在HttpHander中重写岂不更好?
      

  7.   

    <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来代替,这样当搜索引擎来抓起你的网页的时候,就会收录你这些网址下去,别人搜索到你网页的时候,就可以从这些地址链接过来。呵呵,是不是很方便呢?不需要修改任何程序,也不需要再占用多余的网络空间,轻松对付搜索引擎。
      

  8.   

    不一定要用HttpModule 实现只要能实现就行了,url重写任意二级域名
      

  9.   

    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
        }
    }