【问题标题】:Asp.net 301 Redirect With Multiple QueryString Parameters具有多个 QueryString 参数的 Asp.net 301 重定向
【发布时间】:2017-12-03 19:15:56
【问题描述】:

我有类似的网址

domain.com/posts/id/title

我想更改网址

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,并且我有两个参数

【问题讨论】:

    标签: asp.net redirect url-rewriting web-config query-string


    【解决方案1】:

    在同一个域中,使用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-id url 发送到处理程序进行处理。您可以这样做(假设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>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-25
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 2016-10-09
    • 1970-01-01
    相关资源
    最近更新 更多