Asp.net 使用多个 QueryString 参数的 301 重定向

Asp.net 301 Redirect With Multiple QueryString Parameters

我有url个赞

domain.com/posts/id/title

我要换URL

domain.com/title-xxx-id

id 和 title 查询字符串参数。 -xxx- 是静态的。

我用过

 <rule name="Redirect to WWW" stopProcessing="true">

  <match url=".*" />

  <conditions>

    <add input="{HTTP_HOST}" pattern="^olddomain.com$" />

  </conditions>

  <action type="Redirect" url="https://newdomain.com/{R:0}"

       redirectType="Permanent" />

</rule>

要更改域,但现在我需要 url 在同一域中进行更改,并且我有两个参数

在同一域中,使用 URLRewrite (2.x) 重定向甚至更简单。像这样。

  <rule name="friendly" stopProcessing="true">
    <match url="^post/(.+)/(.+)$" negate="false" />
    <action type="Redirect" url="{R:1}-xxx-{R:2}" appendQueryString="false" />
  </rule>

但实际上需要将title-xxx-idurl发送给handler来处理。你可以这样做(假设 post 是你使用的控制器)。

  <rule name="friendly1" stopProcessing="true">
    <match url="^(.+)-xxx-(\d+)$" negate="false"/>
    <action type="Rewrite" url="/post?title={R:1}&amp;id={R:2}" appendQueryString="false"/>
  </rule>

事实上,这两个规则可以一起使用。

  <rule name="friendly" stopProcessing="false">
    <match url="^post/(.+)/(.+)$" negate="false" />
    <action type="Redirect" url="{R:1}-xxx-{R:2}" appendQueryString="false" />
  </rule>
  <rule name="friendly1" stopProcessing="true">
    <match url="^(.+)-xxx-(\d+)$" negate="false"/>
    <action type="Rewrite" url="/post?title={R:1}&amp;id={R:2}" appendQueryString="false"/>
  </rule>