如何通过web.config从URL中删除查询字符串

时间:2015-01-20 09:04:58

标签: c# asp.net redirect

我需要301重定向所有网址,例如

  

xyz.com/searchitems?key1=val1&key2=val2就像

     

xyz.com/searchitems

即。我想要从URL中删除查询字符串。到目前为止,我已经在web.config

中写了这篇文章
        <rewrite>
      <rules>
        <rule name="Rewrite to searchitems">
          <match url="^searchitems?$" />
<conditions>
    <add input="{QUERY_STRING}" pattern="(.*)" />
  </conditions>
          <action type="Redirect" redirectType="Permanent" url="{R:0}" appendQueryString="false"  />
        </rule>
      </rules>
   </rewrite>

但它正在制作重定向循环

编辑:

我只为此网址“xyz.com/searchitems”删除了查询字符串,而不是所有网址。网址中可以有任意数量的查询字符串参数。

1 个答案:

答案 0 :(得分:2)

试试这个:

  <rewrite>
      <rules>
        <rule name="Custom rule" stopProcessing="true">
          <match url="^searchitems" />
          <conditions>
            <add input="{QUERY_STRING}" pattern="(.+)" />
          </conditions>
          <action type="Redirect" url="/searchitems" appendQueryString="false" />
        </rule>
      </rules>
    </rewrite>