利用Intelligencia.UrlRewriter.dll实现URL重写2

来源:互联网 发布:mac下载文件夹不见了 编辑:程序博客网 时间:2024/04/24 09:10

 

1、http://urlrewriter.net/   下载解压缩后把Intelligencia.UrlRewriter.dll 文件添加引用到web工程里。

2.web.config配置:

<configSections>
   <!--UrlRewriter-->
   <section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
</configSections>

<system.web>
   <!--UrlRewriter-->
   <httpModules>
    <add type="Intelligencia.UrlRewriter.RewriterHttpModule,Intelligencia.UrlRewriter" name="UrlRewriter" />
         </httpModules>
</system.web>
</system.web>

<!--UrlRewriter-->
<rewriter>
   <rewrite url="~/test_([a-zA-Z]+).html$" to="~/Show.aspx?Code=$1" processing="stop" />
</rewriter>

其中: <rewrite url="~/test_([a-zA-Z]+).html$" to="~/Show.aspx?Code=$1" processing="stop" />

若果code= abc的话,最终显示的结果为"虚拟目录名称/test_abc.html" 。如果吧.html替换成 .aspx那就是"虚拟目录名称/test_abc.aspx"

3.IIS配置:
如果是用"虚拟目录名称/test_abc.aspx" 的话,不需要配置IIS. 使用了"虚拟目录名称/test_abc.html" 配置IIS的方法如下:
http://urlrewriter.net/index.php/support/installation/windows-server-2003/ (注意: 按照这个一步一步的做,不管下面的图,下面的图看不清)里面的图9不准确,准确的为:

那个文章里没说清楚的部分,应该是asax,而不是asa选项

还有第八步中的”untick Verify that file exists.“要看仔细。否则重写HTML就无效。

4:备注

不要在"应用程序扩展"里“添加" .html的映射,否则正常的html将无法访问。

5.在vs2005中,传多个参数应该加&amp;在vs2003中,只用分号就好了

eg.

<RewriterConfig>
    <Rules>
      <RewriterRule>
        <!--CreditNews BackGround update-->
        <LookFor>~NewsClsManage-(/d*)-(/d*).html</LookFor>
        <SendTo>~NewsClsManage.aspx?ClassID=$1&amp;flag=$2</SendTo>
      </RewriterRule>
    </Rules>
</RewriterConfig>

注意 接连传参是 要用&amp;而不是&。。。。。

下面是写的一个参考实例

  <rewriter>
    <rewrite url="~/default.html$" to="~/default.aspx" processing="stop" />
    <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>