利用404错误实现地址重写

工作日记,防止自己电脑中毒,给自己看,比较乱,来客莫怪

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&amp;SClass=$3&amp;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;
        }

    }


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值