简体   繁体   English

IIS URL重写模块:基于QueryString的重定向

[英]IIS URL Rewrite Module : Redirect Based On QueryString

I Have some problems with redirecting to another URL based on the query string parameters. 我在根据查询字符串参数重定向到另一个URL时遇到一些问题。 I want to redirect users which enter www.domain.com/signup.aspx?p=1 to: 我想将输入www.domain.com/signup.aspx?p=1的用户重定向到:

www.domain.com/signup 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.aspx?p=2时,他们必须去:

www.domain.com/signup/promocode 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 GR

Martijn 马亭

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. 对于每个新路径,这不需要新规则(以及针对每个请求上的模式的URL的额外评估)。

<rules>
  <rule name="Signup Redirect Map" stopProcessing="true">
    <match url="^signup\.aspx$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
      <add input="{QUERY_STRING}" pattern="p=([^&amp;]+)" />
      <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. {C:1}是对第一个条件匹配的反向引用:查询字符串值。
  • {Signups:{C:1}} is an instruction to look up {C:1} in the Signups map. {Signups:{C:1}}是在Signups地图中查找{C:1}的指令。
  • {C:2} is a backreference to the second condition match: the value from the Signups map. {C:2}是对第二个条件匹配的反向引用:来自Signups映射的值。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM