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

Redirect URL when last character of URL matches a regex

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

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 末尾有冒号时会发生这种情况,这可能是由于电子邮件软件在电子邮件中包含冒号 "my site is at www.someurl.com: you'll find the info".

我想将每个以冒号结尾的 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>

您可以试试这个重写规则。

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