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); 
    	} 
    } 

  • 相关阅读:
    Python之迭代器,生成器
    Python函数--装饰器进阶
    Python之函数的本质、闭包、装饰器
    Python之函数--命名空间、作用域、global、nonlocal、函数的嵌套和作用域链
    Python函数的定义与调用、返回值、参数
    Python之文件操作
    Python之集合
    基本数据类型补充,深浅copy
    Python基础-元组、列表、字典
    Python常用模块(一)
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234195.html
Copyright © 2011-2022 走看看