通过 web.config 删除查询字符串

Lar*_*ard 2 c# asp.net iis web-config url-rewrite-module

在我的网站上,我遇到这样的情况:我的 URL 有时会获得由我们网站之外的系统附加的查询字符串参数。

因此,它不是像这样:http://www.domain.com?myquery=blahor http://www.domain.com,而是 url http: http://www.domain.com?myquery=blah&theirpara=blahor http://www.domain.com?theirpara=blah

当用户使用“irpara”参数访问时,我想将 301 重定向到没有它的 URL。

我尝试过使用 URL 重写模块,但没有真正取得任何进展。如果可能的话,最好在 IIS/web.config 级别而不是 Response.RedirectPermanent 级别执行此操作。

我认为使用规则(URL 写入模块)进行设置会很好,但说实话,我不知道如何解决这个问题。我正在使用以下规则删除尾部斜杠,但不确定如何修改它以满足此需要。

<!--To always remove trailing slash from the URL-->
<rule name="Remove trailing slash" stopProcessing="true">
  <match url="(.*)/$" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
  </conditions>
  <action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
Run Code Online (Sandbox Code Playgroud)

关于如何设置它有什么想法吗?

str*_*stn 5

如果您正在寻找一个非常通用的规则来删除theirpara但保留任何其他查询字符串参数,那么您将不得不处理以下情况

  1. 它是孤独的:?theirpara=123->/
  2. 这是第一个:?theirpara=123&ourparams=456->?ourparams=456
  3. 它在中间:?our1=123&theirpara=456&our2=789->?our1=123&our2=789
  4. 这是最后一个:?ourparams=123&theirpara=456->?ourparams=123

我编写了一条处理这些情况的规则,但在情况 4 中留下了一个尾随的 & 符号,但我希望它足以让您开始。

<rule name="Redirect on theirpara" stopProcessing="true">
    <match url="^(.*)$" />
        <conditions>
            <add input="{QUERY_STRING}" pattern="^(.*)theirpara=([^=&amp;]+)&amp;?(.*)$" />
        </conditions>
    <action type="Redirect" url="/{R:0}?{C:1}{C:3}" appendQueryString="false" />
</rule>
Run Code Online (Sandbox Code Playgroud)