zoukankan      html  css  js  c++  java
  • 使用Intelligencia.UrlRewriter重写有后缀及无后缀的url

    如果使用URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter实现有后缀和无后缀的url是比较麻烦的,原因是因为样式及图片路径的问题。

    后来选择Intelligencia.UrlRewriter来实现,比前者方便多了。

    步骤:

    1,下载Intelligencia.UrlRewriter

    2,在项目引用

    3,webconfig配置:

    <configSections>
    <section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
    </configSections>
    <rewriter>
    <rewrite url="^/404.html/?$" to="/404.aspx" processing="stop" />
    <rewrite url="^/?$" to="/Default.aspx" processing="stop" />
    </rewriter>
    <httpModules>
    <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />
    </httpModules>
    <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
    <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />
    </modules>
    <validation validateIntegratedModeConfiguration="false"/>
    </system.webServer>

    4,Intelligencia.UrlRewriter在IIS7、7.5中的配置方法:

          在vista、win7、server2008中,iis的版本是v7+,IIS6重写配置在IIS7中重写无效解决方案由于IIS6只能在请求被分配到 Asp.Net引擎后才能发生重写操作,IIS7可以在IIS请求管道的任何地方执行一个HttpModule, 而且IIS7对于文件或文件夹的访问权限得重新设置.

          如果是IIS7.0/7.5的运行环境,添加该配置

        

    <system.webServer>

     <modules>

     <!--您的自定义IIS重写模块操作-->

     <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule"/>

     </modules>

     </system.webServer>

    5,在server2000,2003服务器上的配置:

      IIS—》网站—》属性—》主目录—》配置—》通配符应用程序映射(执行顺序)—》插入—》浏览找到asp.net_isapi.dll—》确定文件是否存在不选择—》确定

      asp.net_isapi.dll(地址:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll),值得注意的是如果你以前加过“应用程序拓展”,要记得把他删除。

    做到以上几步不管是在iis7还是在windowserver服务器上都可以访问了。

    以下为补充:(摘自http://www.cnblogs.com/Fred_Xu/archive/2009/11/22/intelligencia-urlrewriter-resource.html)


    参考博文:http://devtalk.dk/2007/03/19/Wildcard+Mapping+And+URL+Rewriting+On+IIS7.aspx 【推荐】

            http://aspalliance.com/1234_TipTrick_Url_Rewriting_with_ASPNET.3

          http://www.improve.dk/blog/2006/12/11/making-url-rewriting-on-iis7-work-like-iis6

    4.重写HTML的规则示例:

    (1)

    注意:

    当使用通配符映射或者使用ASP.NET处理所有HTTP请求的时候,IIS的默认文档机制也就失去了该有的作用,以下代码就是用来重新实现默认文档的方法:

    <rewrite url="^(.*)/(\?.+)?$" to="$1/default.aspx$2" />注意:使用“processing="restart"”的时候将会导致重写引擎从头开始执行所有的重写规则,此时应当注重包含“ /default.aspx”字符串的处理。

    如果你需要同时支持多个默认文档,那么以上代码需要修改为:

    <if url="^(.*)/(\?.+)?$">

     <rewrite exists="$1/default.aspx" to="$1/default.aspx$2" />

     <rewrite exists="$1/index.aspx" to="$1/index.aspx$2" />

     <rewrite exists="$1/index.html" to="$1/index.html$2" />

    </if>编辑以上规则代码的时候,尤其要注意确认文件是否存在。

    此外,在使用通配符映射或者使用ASP.NET处理所有HTTP请求的时候,你会发现.gif、.css等文件无法正常访问,这是因为此时对这些文件类型的处理都被ASP.NET所拦截。修正方法如下:

    <rewrite

     url="^(/.+(\.gif|\.png|\.jpg|\.ico|\.css|\.js)(\?.+)?)$"

     to="$1" processing="stop" />两个比较特殊的正则表达式字符是“^”和“$”(不是必须的),“^”代表URL的开头,“$”代表URL的结尾。使用这两个符号可以使您更加 精确的控制重写动作,以确保程序所匹配的URL正是您想处理的。

    “~/”表示当前网站应用程序运行所在的虚拟根目录,当您把网站应用程序安装于虚拟目录(或者非根目录)的时候,这尤其有用,而无须重新编写任何代码来替换您的虚拟根路径。

    模式匹配在处理查询字串(QueryStrings)的时候尤其有用,这可以让您的URL去掉类似于“?id=3”的代码段,这非常有用,可以让你轻松的实现伪静态。

    下面列出两个我们认为非常有特色的重写规则:

    (1) 当目标URL不包含自定义查询字串的时候:

    <rewrite url="^~/mypage(\?.+)?$" to="~/default.aspx$1" />此时“$1”匹配的是(\?.+)?,也就是所有的查询字串。

    (2) 当目标URL包含自定义查询字串的时候:

    <rewrite url="^~/mypage(\?(.+))?$"

     to="~/default.aspx?page=mypage&$2" />注意此时需要使用“&amp;”来代替“&”,这是XML文档语法的需要。

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/bl_song/archive/2009/11/03/4761343.aspx

    (2)

    <rewriter>
        
    <rewrite url="~/default.html$" to="~/default.aspx" processing="stop" />
        
    <rewrtie url="^~/(\d+)/(\d+).html" to="~/Default.aspx?id=$1&amp;artcleid=$2" />//多个参数
        
    <rewrite url="~/news_([0-9]+).html$" to="~/Article/ArticleShow.aspx?id=$1" processing="stop" />
        
    <rewrite url="~/Result_([0-9]+).aspx$" to="~/Job/Company/Result.aspx?cid=$1" processing="stop" />
        
    <rewrite url="~/company_([0-9]+).html$" to="~/Company/$1/index.html" processing="stop" />
        
    <rewrite url="~/Resume/([0-9]+)_([0-9]+).html$" to="~/Resume/$1/Resume_$2.html" processing="stop" />
        
    <rewrite url="~/([0-9]+)_([0-9]+).html$" to="~/company/$1/job_$2.html" processing="stop" />
        
    <rewrite url="~/job_([0-9]+).html$" to="~/company/$1/jobs_$1.html" processing="stop" />
        
    <rewrite url="~/news_l([0-9]+)_1.html$" to="~/company/$1/$1_news.html" processing="stop" />
        
    <rewrite url="~/news_([0-9]+)v([0-9]+).html$" to="~/company/$1/n_$2.html" processing="stop" />
        
    <rewrite url="~/contact_([0-9]+).html$" to="~/company/$1/contact.html" processing="stop" />
        
    <rewrite url="~/a_(.+)_(.+).aspx$" to="~/a.aspx?id=$1&amp;id1=$2" processing="stop" />//多个参数
     
    </rewriter> 

    5.博客园资源整理:

    JeffreyZhao 赵劼的博文:

    重提URL Rewrite(1):IIS与ASP.NET

    重提URL Rewrite(2):使用已有组件进行URL Rewrite

    重提URL Rewrite(3):在URL Rewrite后保持PostBack地址 //这篇文章对我帮助最大了,在实际项目中是一定要用到的。

    重提URL Rewrite(4):不同级别URL Rewrite的一些细节与特点

    6.Intelligencia.UrlRewriter和fck editor冲突问题:

      在IIS7/7.5环境中,,建议使用集成模式,不要使用Classic模式,否则UrlRewriter和fck editor会有冲突,造成fck editor的页面无法执行URL。


      

  • 相关阅读:
    Yield Usage Understanding
    Deadclock on calling async methond
    How to generate file name according to datetime in bat command
    Run Unit API Testing Which Was Distributed To Multiple Test Agents
    druid的关键参数+数据库连接池运行原理
    修改idea打开新窗口的默认配置
    spring boot -thymeleaf-url
    @pathvariable和@RequestParam的区别
    spring boot -thymeleaf-域对象操作
    spring boot -thymeleaf-遍历list和map
  • 原文地址:https://www.cnblogs.com/sheseido/p/2382032.html
Copyright © 2011-2022 走看看