Asp.net 将查询字符串附加到IIS重写映射

Asp.net 将查询字符串附加到IIS重写映射,asp.net,iis,url-rewriting,iis-7,Asp.net,Iis,Url Rewriting,Iis 7,我有一个重写映射,我想将请求的URL中的任何查询参数附加到重写的URL 例如: /page/abc/-->/index.cfm?page=abc(工程) /page/abc/?param1=111-->/index.cfm?page=abc¶m1=111(不工作) /page/abc/?param3=333¶m4=444-->/index.cfm?page=abc¶m3=333¶m4=444(不工作) 我的web.config是: [...] <rule

我有一个重写映射,我想将请求的URL中的任何查询参数附加到重写的URL

例如:

  • /page/abc/-->/index.cfm?page=abc(工程)
  • /page/abc/?param1=111-->/index.cfm?page=abc¶m1=111(不工作)
  • /page/abc/?param3=333¶m4=444-->/index.cfm?page=abc¶m3=333¶m4=444(不工作)
我的web.config是:

[...]
<rules>
    <clear />
    <rule name="Rewrite rule1 for SiteMapEngine">
        <match url=".*" />
        <conditions>
            <add input="{SiteMapEngine:{REQUEST_URI}}" pattern="(.+)" />
        </conditions>
        <action type="Rewrite" url="{C:1}" appendQueryString="true" />
    </rule>
</rules>
[...]
[…]
[...]

这是我的规则。它似乎如预期的那样起作用:

<rule name="Insert index.cfm" enabled="true" stopProcessing="true">
    <match url="^(.*)$" ignoreCase="false" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="index.cfm/{PATH_INFO}" appendQueryString="true" />
</rule> 

如果我能找到这方面的参考,我会被诅咒,但据我所知,在某些版本的IIS中,{REQUEST_URI}返回时没有查询字符串,如果启用重写,则返回时将完全为空

您应该可以改为使用{PATH_INFO}

这个bug报告(针对Drupal!)就是您描述的问题,我认为:


Microsoft提供了一个修补程序,但我没有尝试过:

简短回答:

使用PATH_INFO服务器变量而不是REQUEST_URI,因为您不希望在匹配中包含查询字符串

完整解释:

这一点我以前就注意到了——基本上,这是在IIS URL重写模块中使用的一个微妙之处

在您的情况下,SiteMapEngine将是URL的静态键值列表:

<rewrite>
    <rewriteMaps>
        <rewriteMap name="SiteMapEngine" defaultValue="">
            <add key="/page/abc/" value="/index.cfm?page=abc" />
            ...
        </rewriteMap>
    </rewriteMaps>
    ...
</rewrite>
请注意,此变量包含查询字符串-因此无法找到匹配的键

相反,请使用PATH_INFO server变量,该变量相当于请求URI,但不包含查询字符串:

{PATH_INFO} = /page/abc/
因此,正确的规则是:

<rule name="Rewrite rule1 for SiteMapEngine">
    <match url=".*" />
    <conditions>
        <add input="{SiteMapEngine:{PATH_INFO}}" pattern="(.+)" />
    </conditions>
    <action type="Rewrite" url="{C:1}" />
</rule>


精彩的解释。要完全回答最初的问题,你还需要记住在动作标签中添加appendQueryString=“true”。哦,天哪,这是怎么救了我的命的。
<rule name="Rewrite rule1 for SiteMapEngine">
    <match url=".*" />
    <conditions>
        <add input="{SiteMapEngine:{PATH_INFO}}" pattern="(.+)" />
    </conditions>
    <action type="Rewrite" url="{C:1}" />
</rule>