【问题标题】:How to handle special characters in web.config rewrite rule?如何处理 web.config 重写规则中的特殊字符?
【发布时间】:2023-03-11 03:49:01
【问题描述】:

当 URL 包含特殊字符时,我的重写规则出现错误:

此网址http://www.example.com/bungalow/rent/state/texas/street/exloër/exloër 使用此重写规则:

    <rule name="rentals by proptype+state+city+street">
      <match url="^([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Zë-+]+)/([0-9a-zA-Zë-+']+)$" />
      <action type="Rewrite" url="search_new.aspx?proptype={R:1}&amp;state={R:2}&amp;city={R:3}&amp;street={R:4}" />
    </rule>

导致 500 错误

此网址http://www.example.com/bungalow/rent/state/texas/street/exloër/exloër 使用此重写规则:

    <rule name="rentals by proptype+state+city+street">
      <match url="^([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Z-+]+)/([0-9a-zA-Z-+']+)$" />
      <action type="Rewrite" url="search_new.aspx?proptype={R:1}&amp;state={R:2}&amp;city={R:3}&amp;street={R:4}" />
    </rule>

导致 404 错误

如何处理重写规则中的特殊字符?

更新 1

有问题的 URL 以 ë 字符显示,但是当我复制地址时,它被转义为 %c3%abr 使用此规则,我仍然会收到 404 错误:

    <rule name="rentals by proptype+state+city+street">
      <match url="^([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Z%-+]+)/([0-9a-zA-Z%-+']+)$" />
      <action type="Rewrite" url="search_new.aspx?proptype={R:1}&amp;state={R:2}&amp;city={R:3}&amp;street={R:4}" />
    </rule>

所以我想真正的问题是,如何处理重写规则中的% 字符?

【问题讨论】:

  • 你没有。 URL 中不允许使用这些内容。您必须先将它们转义,然后才能将它们变成 url。
  • @VDWWD 谢谢。我确实在使用那个模块。但是在阅读该页面后,我仍然无法解释为什么我的链接不起作用并引发错误。你能解释一下吗?

标签: asp.net url-rewriting web-config special-characters


【解决方案1】:

您上次尝试的正则表达式几乎正确,您只是犯了一个小错误(忘记将 0-9 添加到第三个块)。正确的正则表达式是:

^([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Z0-9%-+]+)/([0-9a-zA-Z%-+']+)$

但是在重写规则中你需要使用变量{UNENCODED_URL}

工作示例是:

<rule name="rentals by proptype+state+city+street" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{UNENCODED_URL}" pattern="^/([a-zA-Z0-9-+]+)/rent/state/([a-zA-Z-+]+)/street/([a-zA-Z0-9%-+]+)/([0-9a-zA-Z%-+']+)$" />
    </conditions>
    <action type="Rewrite" url="search_new.aspx?proptype={C:1}&amp;state={C:2}&amp;city={C:3}&amp;street={C:4}" />
</rule>

UPD

来自 cmets 的示例之后:

您的网址:http://www.example.com/bungalow/rent/state/north-dakota/stre‌​‌​et/savanah/%27s-gr‌​ac‌​eland 有一些隐藏的特殊字符(即使 SO 也无法正确解析)。您可以在此处查看其编码方式:https://www.urlencoder.org/

因为我改变了规则中的正则表达式如下:

<rule name="rentals by proptype+state+city+street" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{UNENCODED_URL}" pattern="^/([a-zA-Z0-9\-+]+)/rent/state/([a-zA-Z\-+]+)/([a-zA-Z0-9%\-+]+)/([a-zA-Z0-9%\-+]+)/([0-9a-zA-Z%\-+']+)$" />
    </conditions>
    <action type="Rewrite" url="search_new.aspx?proptype={C:1}&amp;state={C:2}&amp;city={C:4}&amp;street={C:5}" />
</rule>

【讨论】:

  • 好吧,它在我提供的示例 URL 上工作,但为什么你的重写规则仍然在这个 URL 上抛出 404:http://www.example.com/bungalow/rent/state/north-dakota/stre‌​et/savanah/%27s-grac‌​eland 和这个(不确定查询字符串参数是否影响你的规则)@ 987654329@
  • 对于使用此规则的任何人,请注意{UNENCODED_URL} 也可能包含查询字符串。因此,当 URL 包含任何查询字符串时,即使它只是一个查询分隔符 (?),也会违反规则。见这里stackoverflow.com/questions/46229792/…
猜你喜欢
  • 2010-11-10
  • 2013-09-09
  • 1970-01-01
  • 2015-09-29
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多