工作日记,防止自己电脑中毒,给自己看,比较乱,来客莫怪
1.首先配置IIS 将404 错误 URL 设定为 Rewriter.aspx
2.将WDFrog.UrlRewriter.dll 文件复制到bin目录下
3.ReWriter.aspx 文件中的代码如下
采用单页形式,需要导如名明空间
<%@ Import Namespace="WDFrog.UrlRewriter" %>
protected void Page_Load(object sender, EventArgs e)
{
string url = Request.Url.ToString();
url = url.Substring(url.IndexOf("404;") + 4);
url = url.Replace(":80", string.Empty);
url = Url404Helper.GetUrl(url, "/default.aspx");
Response.Write(url);
//Server.Transfer(url);
}
4.web.Config文件配置如下 按正则表达式来的
<configuration>
<configSections>
<section name="url404" type="WDFrog.UrlRewriter.URL404,WDFrog.UrlRewriter"/>
</configSections>
<url404 >
<rules>
<add name="rule3" fromUrl="http://(.*?)/.{1}xxx.net/OTC/(/d+)/*" toUrl="/otc/list.aspx?page=$2" enabled="true" />
<add name="rule1" fromUrl="http://(.*?)/.{1}xxx.net/b(/d+)/s(/d+)/(/d+)" toUrl="/zhao/detail.aspx?BClass=$2&SClass=$3&Id=$4" enabled="true" />
<add name="rule2" fromUrl="http://(www/.)*?xxx.net/otc/(/d+)/*" toUrl="/otc/list.aspx?page=$2" enabled="true" />
</rules>
</url404>
<appSettings/>
...........
</configuration>
文件上传不了,鄙视csdn...
代码如下
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Text.RegularExpressions;
using
System.Configuration;
namespace
WDFrog.UrlRewriter

...
{

/**//// <summary>
/// 首先将IIS的404错误定向到指定的页面
/// 配置文件格式
/// <configSections>
/// <section name="url404" type="URL404"/>
///</configSections>
/// <url404 >
/// <rules>
/// <add name="rule3" fromUrl="http://(.*?).{1}gyzs.net/OTC/(d+)/*" toUrl="/otc/list.aspx?page=$2" enabled="true" />
/// <add name="rule1" fromUrl="http://(.*?).{1}gyzs.net" toUrl="/$1/" enabled="true" />
/// <add name="rule2" fromUrl="http://(www.)*?gyzs.net/otc/(d+)/*" toUrl="/otc/list.aspx?page=$2" enabled="true" />
/// </rules>
/// </url404>
/// </summary>
public class URL404 : ConfigurationSection

...{
public URL404()

...{
//
// TODO: 在此处添加构造函数逻辑
//
}
[ConfigurationProperty("rules")]
public ConfigurationElemenRuleCollection Rules

...{

get ...{ return (ConfigurationElemenRuleCollection)this["rules"]; }

}
[ConfigurationProperty("errorUrl")]
public string ErrorUrl

...{

get ...{ return (string)this["errorUrl"]; }

set ...{ this["errorUrl"] = value; }
}
}
public class ConfigurationElementRule : ConfigurationElement

...{
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name

...{

get ...{ return (string)this["name"]; }

set ...{ this["name"] = value; }
}

[ConfigurationProperty("fromUrl", IsRequired = true)]
public string FromUrl

...{

get ...{ return (string)this["fromUrl"]; }

set ...{ this["fromUrl"] = value; }
}
[ConfigurationProperty("toUrl", IsRequired = true)]
public string ToUrl

...{

get ...{ return (string)this["toUrl"]; }

set ...{ this["toUrl"] = value; }
}
[ConfigurationProperty("enabled")]
public bool Enabled

...{

get ...{ return (bool)this["enabled"]; }

set ...{ this["enabled"] = value; }
}
}
public class ConfigurationElemenRuleCollection : ConfigurationElementCollection

...{
protected override ConfigurationElement CreateNewElement()

...{
return new ConfigurationElementRule();

}
protected override object GetElementKey(ConfigurationElement element)

...{
return ((ConfigurationElementRule)element).Name;
}
public ConfigurationElementRule this[int index]

...{

get ...{ return (ConfigurationElementRule)BaseGet(index); }
}
public ConfigurationElementRule this[string name]

...{

get ...{ return (ConfigurationElementRule)BaseGet(name); }
}

}
public static class Url404Helper

...{
public static string GetUrl(string url, string defaultUrl)

...{

string retUrl = defaultUrl;
URL404 section = (URL404)ConfigurationManager.GetSection("url404");
try

...{
foreach (ConfigurationElementRule rule in section.Rules)

...{
if (Regex.IsMatch(url, rule.FromUrl, RegexOptions.IgnoreCase | RegexOptions.Singleline) & rule.Enabled)

...{
retUrl = Regex.Replace(url, rule.FromUrl, rule.ToUrl, RegexOptions.IgnoreCase | RegexOptions.Singleline);
break;
}
}
}
catch (Exception ex)

...{
if (!string.IsNullOrEmpty(section.ErrorUrl))
retUrl = section.ErrorUrl;
}
return retUrl;
}
}

}