zoukankan      html  css  js  c++  java
  • 使用微软URLRewriter.dll的url实现任意后缀名重写

    <?xml version="1.0"?>
    <!--先引用URLRewriter.dll,放置于Bin目录-->
    <configuration>
    	<configSections>
    		<!--配置重写规则节点-->
    		<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>
    	</configSections>
    	<connectionStrings>
    		<add name="TestConnectionString" connectionString="Data Source=.;Initial Catalog=Test;Integrated Security=True" providerName="System.Data.SqlClient"/>
    	</connectionStrings>
    	<!--重写规则-->
    	<RewriterConfig>
    		<Rules>
    			<RewriterRule>
    				<LookFor>~/product/([0-9]*).html</LookFor>            <!--http://www.xxx.com/product/8001.html-->
    				<SendTo>~/product.aspx?pid=$1</SendTo>                 <!--http://www.xxx.com/product.aspx?pid=8001-->
    			</RewriterRule>
    			<RewriterRule>
    				<LookFor>~/product/([0-9]*)-([0-9]*).html</LookFor>   <!--http://www.xxx.com/product/101-8001.html-->
    				<SendTo>~/product.aspx?sid=$1&pid=$2</SendTo>      <!--http://www.xxx.com/product.aspx?sid=101&pid=8001-->
    			</RewriterRule>
    			<RewriterRule>
    				<LookFor>~/product/([0-9]*)/([0-9]*).html</LookFor>   <!--http://www.xxx.com/product/101/8001.html-->
    				<SendTo>~/product.aspx?sid=$1&pid=$2</SendTo>      <!--http://www.xxx.com/product.aspx?sid=101&pid=8001-->
    			</RewriterRule>
    		</Rules>
    	</RewriterConfig>
    	<system.web>
    		<httpHandlers>
    			<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter"/>
    			<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter"/>
    		</httpHandlers>
    		<compilation debug="true" targetFramework="4.0"/>
    	</system.web>
    </configuration>
    
    //利用Global.asax的Application_BeginRequest 实现url 重写 无后缀 
    void Application_BeginRequest(object sender, EventArgs e) 
    { 
    	string oldUrl = System.Web.HttpContext.Current.Request.RawUrl; //获取初始url 
    
    	//~/123.aspx → ~/Index.aspx?id=123 
    	Regex reg = new Regex(@"^/d+.html"); 
    	if (reg.IsMatch(oldUrl)) 
    	{ 
    		string id = reg.Match(oldUrl).ToString().Substring(1, reg.Match(oldUrl).ToString().LastIndexOf(".") - 1); 
    		Context.RewritePath("~/Index.aspx?id=" + id); 
    	} 
    
    	//~/123 → ~/Index.aspx?id=123 
    	Regex reg1 = new Regex(@"^/d+$"); 
    	if (reg1.IsMatch(oldUrl)) 
    	{ 
    		string id = reg1.Match(oldUrl).ToString().Substring(1); 
    		Context.RewritePath("~/Index.aspx?id=" + id); 
    	} 
    
    	//~/index/123 → ~/Index.aspx?id=123 
    	Regex reg3 = new Regex(@"^/index/d+$"); 
    	if (reg3.IsMatch(oldUrl)) 
    	{ 
    		string id = reg3.Match(oldUrl).ToString().Substring(7); 
    		Context.RewritePath("~/Index.aspx?id=" + id); 
    	} 
    } 

  • 相关阅读:
    实验0 了解和熟悉操作系统一、目的和要求
    读后感
    有穷自动机自动转化
    文法分析
    词法分析随笔
    git操作笔记
    面试题汇总
    MYSQL数据库设计
    Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
    invalid comparison:java.util.Date and java.lang.String
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234195.html
Copyright © 2011-2022 走看看