IIS URL 重写模块:基于查询字符串重定向

发布于 2024-08-21 14:06:21 字数 810 浏览 9 评论 0原文

我在根据查询字符串参数重定向到另一个 URL 时遇到一些问题。我想将输入 www.domain.com/signup.aspx?p=1 的用户重定向到:

www.domain.com/signup

<rule name="Signup Redirect 1" stopProcessing="true">
  <match url="signup\.aspx\?p=1" />
  <conditions logicalGrouping="MatchAll" />
  <action type="Redirect" url="signup" redirectType="Temporary" />
</rule>

现在,当他们输入 www.domain.com/signup.aspx?p=2 时,他们必须转到:

www.domain.com/signup/promocode

<rule name="Signup Redirect 2" stopProcessing="true">
  <match url="signup\.aspx\?p=2" />
  <conditions logicalGrouping="MatchAll" />
  <action type="Redirect" url="signup/promocode" redirectType="Temporary" />
</rule>

上述规则不起作用。这样做的正确方法是什么?提前致谢。

格·

马丁

I Have some problems with redirecting to another URL based on the query string parameters. I want to redirect users which enter www.domain.com/signup.aspx?p=1 to:

www.domain.com/signup

<rule name="Signup Redirect 1" stopProcessing="true">
  <match url="signup\.aspx\?p=1" />
  <conditions logicalGrouping="MatchAll" />
  <action type="Redirect" url="signup" redirectType="Temporary" />
</rule>

Now when they enter www.domain.com/signup.aspx?p=2 they must go to:

www.domain.com/signup/promocode

<rule name="Signup Redirect 2" stopProcessing="true">
  <match url="signup\.aspx\?p=2" />
  <conditions logicalGrouping="MatchAll" />
  <action type="Redirect" url="signup/promocode" redirectType="Temporary" />
</rule>

The above rules don't work. What is the right way to do this? Thanks in Advance.

Gr

Martijn

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

×纯※雪 2024-08-28 14:06:21

使用值来选择目的地的更可靠方法是使用重写映射。该映射本质上是一个查找表。这不需要为每个新路径创建新规则(以及根据每个请求的模式对 URL 进行额外评估)。

<rules>
  <rule name="Signup Redirect Map" stopProcessing="true">
    <match url="^signup\.aspx$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
      <add input="{QUERY_STRING}" pattern="p=([^&]+)" />
      <add input="{Signups:{C:1}}" pattern="(.+)" />
    </conditions>
    <action type="Redirect" url="{C:2}" redirectType="Temporary" />
  </rule>
</rules>
<rewriteMaps>
  <rewriteMap name="Signups">
    <add key="1" value="signup" />
    <add key="2" value="signup/promocode" />
    <add key="3" value="signup/newcode" />
    <add key="n" value="signup/futureproof" />
  </rewriteMap>
</rewriteMaps>

定义:

  • {C:1} 是对第一个条件匹配的反向引用:查询字符串值。
  • {Signups:{C:1}} 是在 Signups 地图中查找 {C:1} 的指令。
  • {C:2} 是对第二个条件匹配的反向引用:注册映射中的值。

A more robust method of using a value to select a destination is to use Rewrite Maps. The map is essentially a lookup table. This doesn't require a new rule (and an additional evaluation of the URL against a pattern on every request) for every new path.

<rules>
  <rule name="Signup Redirect Map" stopProcessing="true">
    <match url="^signup\.aspx$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
      <add input="{QUERY_STRING}" pattern="p=([^&]+)" />
      <add input="{Signups:{C:1}}" pattern="(.+)" />
    </conditions>
    <action type="Redirect" url="{C:2}" redirectType="Temporary" />
  </rule>
</rules>
<rewriteMaps>
  <rewriteMap name="Signups">
    <add key="1" value="signup" />
    <add key="2" value="signup/promocode" />
    <add key="3" value="signup/newcode" />
    <add key="n" value="signup/futureproof" />
  </rewriteMap>
</rewriteMaps>

Definitions:

  • {C:1} is a backreference to the first condition match: the query string value.
  • {Signups:{C:1}} is an instruction to look up {C:1} in the Signups map.
  • {C:2} is a backreference to the second condition match: the value from the Signups map.
遥远的她 2024-08-28 14:06:21

看看这样是否效果更好一点:

<rule name="Signup Redirect 1" stopProcessing="true">
  <match url="signup\.aspx$" />
  <conditions>
    <add input="{QUERY_STRING}" pattern="p=1" />
  </conditions>
  <action type="Redirect" url="signup" redirectType="Temporary" />
</rule>

<rule name="Signup Redirect 2" stopProcessing="true">
  <match url="signup\.aspx$" />
  <conditions>
    <add input="{QUERY_STRING}" pattern="p=2" />
  </conditions>
  <action type="Redirect" url="signup/promocode" redirectType="Temporary" />
</rule>

See if this works a bit better:

<rule name="Signup Redirect 1" stopProcessing="true">
  <match url="signup\.aspx$" />
  <conditions>
    <add input="{QUERY_STRING}" pattern="p=1" />
  </conditions>
  <action type="Redirect" url="signup" redirectType="Temporary" />
</rule>

<rule name="Signup Redirect 2" stopProcessing="true">
  <match url="signup\.aspx$" />
  <conditions>
    <add input="{QUERY_STRING}" pattern="p=2" />
  </conditions>
  <action type="Redirect" url="signup/promocode" redirectType="Temporary" />
</rule>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文