URL的最后一个字符与正则表达式匹配时重定向URL

时间:2019-03-31 13:37:23

标签: asp.net url web-config

我在日志中注意到以下错误:

Exception Type: 
System.Web.HttpException
Exception: A potentially dangerous Request.Path value was detected from the client (:).
Stack Trace: 
at System.Web.HttpRequest.ValidateInputIfRequiredByConfig() at System.Web.HttpApplication.PipelineStepManager.ValidateHelper(HttpContext context)

这种情况发生在URL的末尾,这可能是由电子邮件软件引起的,该软件在以表示的电子邮件中包含冒号,“我的网站位于www.someurl.com:找到信息”。

我想将每个以冒号结尾的URL重写并重定向到相同的URL,而冒号不在最后一个位置。

这就是我所拥有的:我在web.config中添加的条目

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Rewrite without last colon">
        <match url="[:]\z" /> //not sure this is correct
        <action type="Rewrite" url="not sure what to put" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

1 个答案:

答案 0 :(得分:1)

您可以尝试使用此重写规则。

<rule name="Remove colon" stopProcessing="true">
  <match url="(.*):$" />
  <action type="Rewrite" url="{R:1}" />
</rule>

(.*) = everything before the :
$ = end of the string to be matched

参考

https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference

https://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/