带二级域名的重写

带二级域名的重写

第一步:配置web.config

 <configSections>
    <section name="GnhaoCustomSection" type="zj123.UI.UrlRewriteHandler"/>
  </configSections>
  <GnhaoCustomSection>
    <UrlRewriteSection>
        <Rule>
        <LookFor><![CDATA[([/w]+).(html|jsp|php)$]]></LookFor>
        <SendTo><![CDATA[index.aspx]]></SendTo>
      </Rule>
      <Rule>
        <LookFor><![CDATA[([/w]+)-([/d]+).(php|jsp|html|about)$]]></LookFor>
        <SendTo><![CDATA[index.aspx]]></SendTo>
      </Rule>
      <Rule>
        <LookFor><![CDATA[([/w]+)-([/d]+).htm$]]></LookFor>
        <SendTo><![CDATA[~/index.aspx?cn=$1&F=$2]]></SendTo>
      </Rule>
      <Rule>
        <LookFor><![CDATA[([/w]+).htm$]]></LookFor>
        <SendTo><![CDATA[~/index.aspx?cn=$1]]></SendTo>
      </Rule>
      <Rule>
        <LookFor><![CDATA[http://(/w+)/.zj123/.com]]></LookFor>
        <SendTo><![CDATA[~/index.aspx?id=$1]]></SendTo>
      </Rule>
      <Rule>
        <LookFor><![CDATA[([/w]+).jsp]]></LookFor>
        <SendTo><![CDATA[~/default.aspx?cn=$1]]></SendTo>
      </Rule>

    </UrlRewriteSection>
    </GnhaoCustomSection>

    <system.web>
      <httpHandlers>
        <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
     validate="false" />
        <add verb="POST,GET" path="*.html,*.htm,*.jsp" type="zj123.UI.UrlRewriteHandler"/>
      </httpHandlers> 

第二步:改iis的isapi的缓存

第三步:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace zj123.UI.App_Code
{

    public sealed class CustomSectionsHandler
    {
        public static XmlNode GetSection(string sectionName, string configFilePath)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(GetCustomSection(sectionName, configFilePath));
            return doc;
        }

        private static string GetCustomSection(string sectionName, string configFilePath)
        {
            string section = string.Empty;
            XmlReaderSettings xrs = new XmlReaderSettings();
            xrs.IgnoreComments = true;
            xrs.IgnoreProcessingInstructions = true;
            xrs.IgnoreWhitespace = true;
            XmlReader xr = XmlReader.Create(configFilePath, xrs);
            while (xr.Read())
            {
                if (xr.NodeType == XmlNodeType.Element)
                {
                    if (xr.Name == sectionName)
                    {
                        section = xr.ReadOuterXml();
                        xr.Close();
                        return section;
                    }
                }
            }
            xr.Close();
            return section;
        }
    }
}

还有一个类

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Web.Caching;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
namespace zj123.UI.App_Code
{
    public class UrlRewriteHandler : IHttpHandlerFactory
    {
        public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string physicalPath)
        {
            string sendUrl = url, sendFilePath = physicalPath;
            if (context.Cache["RewriteRules"] == null)
            {
                context.Cache.Insert("RewriteRules", GetPatternRules());
            }
            ArrayList al = (ArrayList)context.Cache["RewriteRules"];
            //ArrayList al = GetPatternRules();
            for (int i = 0; i < al.Count; i++)
            {
                string[] rule = al[i].ToString().Split("~".ToCharArray());
                Regex rg = new Regex(rule[0], RegexOptions.IgnoreCase);
                if (rg.IsMatch(sendUrl))
                {
                    url = rg.Replace(url, rule[1]);
                    UrlRewrite(context, url, out sendUrl, out sendFilePath);
                    return PageParser.GetCompiledPageInstance(sendUrl, sendFilePath, context);
                   
                }
            }
            return PageParser.GetCompiledPageInstance(sendUrl, sendFilePath, context);
        }

        private void UrlRewrite(HttpContext context, string url, out string sendUrl, out string sendFilePath)
        {
            string queryString = string.Empty;
            sendUrl = url;
            if (url.IndexOf("?") > 0)
            {
                sendUrl = url.Substring(0, url.IndexOf("?"));
                queryString = url.Substring(url.IndexOf("?") + 1);
            }
            sendFilePath = context.Server.MapPath(sendUrl);
            context.RewritePath(sendUrl, string.Empty, queryString);
        }
        private ArrayList GetPatternRules()
        {
            ArrayList al = new ArrayList();
            XmlNode xn = CustomSectionsHandler.GetSection("UrlRewriteSection", HttpContext.Current.Server.MapPath("~/web.config"));
            foreach (XmlNode x in xn.FirstChild.ChildNodes)
            {
                string rule = string.Empty;
                foreach (XmlNode n in x.ChildNodes)
                {
                    rule += n.InnerText;
                }
                al.Add(rule);
            }
            return al;
        }
        public void ReleaseHandler(IHttpHandler hadler)
        {
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值