url重定方法| urlrewrite 在iis上的设置方法

程序要调整的部分只有两块。
一是web.config文件。
二是链接地址。
所需urlrewrite.dll

1.先去下一个文件:MSDNURLRewriting.msi;用来提取URLRewriter.dll这个东西的。在本地安装MSDNURLRewriting.msi之后,找到URLRewriter.dll,然后copy到网站目录任意地方,我是放到Bin里面的。

下载地址:URLRewriter:http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi

2.下面可以配置web.config文件了,要加入的东西如下:

先在 <configSections>中加入

<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />

这段代码记得放最上面,否则出问题才叫麻烦。

然后再在 <configSections>中<configSections>的下面加入

<RewriterConfig>

    <Rules>

      <RewriterRule>

        <LookFor>~/(\d{2})\.html</LookFor>

        <SendTo>~/New/new.aspx?id=$1</SendTo>

      </RewriterRule>

    </Rules>

</RewriterConfig>

这个东西是用来描述重定向规则的,一会再说。

最后再找到<httpHandlers>,在里面的加入

      <add verb="*" path="*.html"

            type="URLRewriter.RewriterFactoryHandler, URLRewriter" />

      <add verb="*" path="*"

            type="URLRewriter.RewriterFactoryHandler, URLRewriter" />

位置不要颠倒,这段代码最好放到<httpHandlers>里面的最下面,省的麻烦。对了,如果里面没有这行代码

<add path="WebResource.axd" verb="GET" type="System.Web.Handlers.AssemblyResourceLoader" validate="True" />

那就记得也要加上,并且要加在最上面。

3.回到刚才那个没有说完的问题上

<RewriterConfig>

    <Rules>

      <RewriterRule>

        <LookFor>~/(\d{2})\.html</LookFor>

        <SendTo>~/New/new.aspx?id=$1</SendTo>

      </RewriterRule>

    </Rules>

</RewriterConfig>

里面的

        <LookFor>~/(\d{2})\.html</LookFor>

        <SendTo>~/New/new.aspx?id=$1</SendTo>

就是用来写url规则的。

其中关键在url的转换

<LookFor>~/(.+).html</LookFor>

<SendTo>~/Shownews.aspx?ShowID=$1</SendTo>

    意思是把第一个路径转成另一个路径。其中<LookFor>()中的正则表达式就是第二句中的参数$1 .

同样也可以用$2 $3来表示<LookFor>中第二 第三个()中的参数。

多个参数:

<ReWriterUrls>

          <rule>

               <old>(.*)/TestUrlRe/file(.*)/(.*)\.html</old>

               <new>../WebForm1.aspx?id=$2&amp;type=$3</new>

          </rule>

          <rule>

               <old>(.*)/TestUrlRe/t(.*)/(.*)\.html</old>

               <new>../WebForm1.aspx?tid=$2&amp;ttype=$3</new>

          </rule>

</ReWriterUrls>

第三步:在页面程序中可以这样写:

<a href="news_<%=newsid%>.html" target="_blank">新闻标题</a>

4.完成上面三个步骤就可以轻松实现URL重写了,不过需要注意的是:如果发布网站的话,你会发现你的URL重写有可能会失效,如果失效的话就需要您设置一下IIS:

打开IIS,主目录-〉配置-〉映射-〉点击“插入”通配符应用程序映射-〉选择“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll”,然后把勾选去掉(一定要去掉),然后确定。(比如我的重定向的后缀名是.html那么就必须要添加扩展名为.html的才行!切记!)

参照

http://smileapple58.blog.163.com/blog/static/98811336201031105353272/

http://blog.csdn.net/ysq5202121/article/details/7004896

原文地址:https://www.cnblogs.com/ishibin/p/2817613.html