带二级域名的重写

带二级域名的重写

第一步:配置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)
{
}
}

}

你可能感兴趣的:(Web,UI,jsp,PHP,Microsoft)