【问题标题】:Append Query String to IIS Rewrite Map将查询字符串附加到 IIS 重写映射
【发布时间】:2015-07-10 03:27:16
【问题描述】:

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

例如:

  • /page/abc/ ---> /index.cfm?page=abc (作品)
  • /page/abc/?param1=111 ---> /index.cfm?page=abc&param1=111 (不起作用)
  • /page/abc/?param3=333&param4=444 ---> /index.cfm?page=abc&param3=333&param4=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>
[...]

【问题讨论】:

    标签: asp.net iis url-rewriting iis-7


    【解决方案1】:

    简答:

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

    完整解释:

    这在我之前就已经引起了我的注意 - 基本上这是在 IIS URL 重写模块中使用 Rewrite Maps 的一个微妙之处。

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

    <rewrite>
        <rewriteMaps>
            <rewriteMap name="SiteMapEngine" defaultValue="">
                <add key="/page/abc/" value="/index.cfm?page=abc" />
                ...
            </rewriteMap>
        </rewriteMaps>
        ...
    </rewrite>
    

    您的规则中的 {SiteMapEngine:{REQUEST_URI}} 条件检查此重写映射中是否有与 REQUEST_URI 服务器变量匹配的键:

    {REQUEST_URI} = /page/abc/?param1=111
    

    请注意,此变量包含查询字符串 - 因此它无法找到匹配的键。

    改为使用 PATH_INFO 服务器变量,它是 REQUEST_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" 添加到操作标签。
    【解决方案2】:

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

    您应该可以改用 {PATH_INFO}。

    这个错误报告(针对 Drupal!)是您描述的问题,我认为:http://drupal.org/node/298016

    有一个来自微软的修补程序,但我没有尝试过:http://support.microsoft.com/kb/954946

    【讨论】:

    • 天哪,这怎么救了我的命。
    【解决方案3】:

    这是我的规则。它似乎按预期工作:

    <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> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-04
      • 2020-03-14
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 2013-08-21
      相关资源
      最近更新 更多