用Intelligencia.UrlRewriter实现URL重写

时间:2022-10-31 10:20:19

 利用Open Source Intelligencia.UrlRewriter实现重写url
      实现效果如下:
  ~/video 到 ~/*.aspx(或者~/tags/*.aspx)
     ~/a.htm(或者自定义的扩展名) 到 ~/a.aspx
     ~/class/(.+)   到  ~/Class/Default.aspx?classid=$1
     等效果

    方法如下:
  (1)下载Intelligencia.UrlRewriter.dll文件
  (2)将Intelligencia.UrlRewriter.dll引用到你的website程序中
  (3)配置webconfig
     1)添加 Configuration section handler:
<configSections>
  <section
   name="rewriter"
   requirePermission="false"
   type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
 </configSections>


     2)添加 UrlRewriter mapper HttpModule:
<system.web>
  <httpModules>
   <add
    type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"
    name="UrlRewriter" />
  </httpModules>
 </system.web>
    3)添加 重写规则到 web.config的configuration结点中,如下
<rewriter>
<rewrite url="/tags/(.+)" to="/tagcloud.aspx?tag=$1" /> 
 </rewriter>
整个web.config文件

 1用Intelligencia.UrlRewriter实现URL重写<?xml version="1.0"?>
 2用Intelligencia.UrlRewriter实现URL重写<configuration>
 3用Intelligencia.UrlRewriter实现URL重写    <configSections>
 4用Intelligencia.UrlRewriter实现URL重写        <section
 5用Intelligencia.UrlRewriter实现URL重写            name="rewriter"
 6用Intelligencia.UrlRewriter实现URL重写            requirePermission="false"
 7用Intelligencia.UrlRewriter实现URL重写            type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
 8用Intelligencia.UrlRewriter实现URL重写    </configSections>
 9用Intelligencia.UrlRewriter实现URL重写    <appSettings/>
10用Intelligencia.UrlRewriter实现URL重写    <connectionStrings/>
11用Intelligencia.UrlRewriter实现URL重写    <system.web>
12用Intelligencia.UrlRewriter实现URL重写        <httpModules>
13用Intelligencia.UrlRewriter实现URL重写            <add
14用Intelligencia.UrlRewriter实现URL重写                type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"
15用Intelligencia.UrlRewriter实现URL重写                name="UrlRewriter" />
16用Intelligencia.UrlRewriter实现URL重写        </httpModules>
17用Intelligencia.UrlRewriter实现URL重写
18用Intelligencia.UrlRewriter实现URL重写    
19用Intelligencia.UrlRewriter实现URL重写        <compilation debug="true"/>
20用Intelligencia.UrlRewriter实现URL重写        
21用Intelligencia.UrlRewriter实现URL重写        <authentication mode="Windows"/>
22用Intelligencia.UrlRewriter实现URL重写
23用Intelligencia.UrlRewriter实现URL重写    </system.web>
24用Intelligencia.UrlRewriter实现URL重写    
25用Intelligencia.UrlRewriter实现URL重写    <rewriter>        
26用Intelligencia.UrlRewriter实现URL重写        <rewrite url="~/tags/(.+)" to="~/tag/tagcloud.aspx?tag=$1" />
27用Intelligencia.UrlRewriter实现URL重写        <rewrite url="~/video" to="~/Class/Default.aspx" />
28用Intelligencia.UrlRewriter实现URL重写        <rewrite url="~/class/(.+)" to="~/Class/Default.aspx?classid=$1" />
29用Intelligencia.UrlRewriter实现URL重写
30用Intelligencia.UrlRewriter实现URL重写        <rewrite url="~/test_([a-zA-Z]+).aspx$" to="~/Show.aspx?Code=$1" processing="stop" />
31用Intelligencia.UrlRewriter实现URL重写        <rewrite url="~/test_([a-zA-Z]+).html$" to="~/Show.aspx?Code=$1" processing="stop" />
32用Intelligencia.UrlRewriter实现URL重写    </rewriter>
33用Intelligencia.UrlRewriter实现URL重写    
34用Intelligencia.UrlRewriter实现URL重写</configuration>
35用Intelligencia.UrlRewriter实现URL重写

  (4)如果需要重写为htm、其它扩展名或者没有扩展名的url时,需要在IIS中设置
   设置方法如下:
        在windowXP系统中:
             1)Open Up IIS and Navigate to the “Home Directory Tab” 
               2)Select “Configuration” 
               3)Click “Add” and enter “C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll” in the Executable box. For the file extension, enter “.*”. Finally, make sure that “Check that file exists” is not checked. 
        在window 2003server中请参考:
        http://urlrewriter.net/index.php/support/installation/windows-server-2003/

        示例代码下载