首页 > 解决方案 > ASP.NET Web.config URL 重写正则表达式

问题描述

我正在尝试清理一些返回 404 而不是 200 的请求。

例如有人有这样的链接到我的网站:

http://example.com/?publisher=123456

我正在尝试添加一个重写规则来处理这种情况,如下所示:

    <rule name="Redirect publisher" stopProcessing="true">
      <match url="(.*)\/\?publisher=(.*)$" negate="false" />
      <conditions logicalGrouping="MatchAny" trackAllCaptures="false"></conditions>
      <action type="CustomResponse" statusCode="404" statusReason="Not Found" statusDescription="The requested URL was not found." />
    </rule>

但是,我遵循了Microsoft 的说明,即使我的测试表明上述正则表达式应该可以工作,但什么也没有发生。我尝试做一个更常见的重定向,如下所示:

<action type="Redirect" url="https://{HTTP_HOST}" redirectType="Permanent" />

然而,又没有任何反应。好像我的正则表达式不好还是什么?

标签: asp.netregexurl-rewriting

解决方案


我终于弄明白了:

    <rule name="Redirect publisher" stopProcessing="true">
      <match url=".*" />
      <conditions>
        <add input="{QUERY_STRING}" pattern="publisher" negate="false" />
      </conditions>
      <action type="CustomResponse" statusCode="404" statusReason="Not Found" statusDescription="The requested URL was not found." />
    </rule>

推荐阅读