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