简体   繁体   English

IIS URL重写-根据URL添加查询字符串

[英]IIS URL rewrite - Add query string depending on url

I have added multiple http bindings to my site like: 我已将多个http绑定添加到我的网站,例如:

http://sub1.domain.com
http://sub2.domain.com
http://sub3.domain.com
http://sub4.domain.com
http://sub5.domain.com

I need to add different query string to those URLs when user hits any of those URLs. 当用户点击任何一个URL时,我需要向这些URL添加不同的查询字符串。

http://sub1.domain.com/?qs=10
http://sub2.domain.com/?qs=15
http://sub3.domain.com/?qs=25
http://sub4.domain.com/?qs=30
http://sub5.domain.com/?qs=50

I'm thinking to keep query string values in appSettings keys. 我正在考虑将查询字符串值保留在appSettings键中。 like 喜欢

  <appSettings>
    <add key ="sub1" value="10" />
    <add key ="sub2" value="15" />
    ...
  </appSettings>

I wrote following rule that appends fixed query string. 我写了以下规则,附加了固定的查询字符串。 But it'll append qs=10 for all five URLs. 但它将为所有五个URL附加qs=10 But I'm clueless about making it dynamic. 但是我对使其动态化一无所知。

<rule name="Add query string param" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{QUERY_STRING}" pattern="qs=10" negate="true" />
            <add input="&amp;{QUERY_STRING}" pattern="^(&amp;.+)|^&amp;$" />
          </conditions>
          <action type="Redirect" url="http://{HTTP_HOST}/{R:0}?qs=10{C:1}" appendQueryString="false"  />
        </rule> 

You should probably look at rewrite maps . 您可能应该看看重写映射 I haven't tested it, but it should be something like this: 我没有测试过,但是应该是这样的:

<rewrite>
 <rewriteMaps>
  <rewriteMap name="SubDomainQueryStrings">
   <add key="sub1.domain.com" value="qs=10" />
   <add key="sub2.domain.com" value="qs=15" />
   <add key="sub3.domain.com" value="qs=25" />
   <add key="sub4.domain.com" value="qs=30" />
   <add key="sub5.domain.com" value="qs=50" />
  </rewriteMap>
 </rewriteMaps>
 <rules>
  <rule name="Add query string param" stopProcessing="true">
   <match url=".*" />
   <conditions>
    <add input="{QUERY_STRING}" pattern="{SubDomainQueryStrings:{HTTP_HOST}}" negate="true" />
    <add input="&amp;{QUERY_STRING}" pattern="^(&amp;.+)|^&amp;$" />
   </conditions>
   <action type="Redirect" url="{R:0}?{SubDomainQueryStrings:{HTTP_HOST}}{C:1}" appendQueryString="false"/>
  </rule>
 </rules>
<rewrite>

You need to use a HTTP_HOST condition to check for the subdomain like that: 您需要使用HTTP_HOST条件来检查子域,如下所示:

<add input="{HTTP_HOST}" pattern="^sub1" />

Code for first two subdomains should look like that: 前两个子域的代码应如下所示:

<rule name="Add query string param 10" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^sub1" />
    <add input="{QUERY_STRING}" pattern="qs=10" negate="true" />
    <add input="&amp;{QUERY_STRING}" pattern="^(&amp;.+)|^&amp;$" />
  </conditions>
  <action type="Redirect" url="http://{HTTP_HOST}/{R:0}?qs=10{C:1}" appendQueryString="false"  />
</rule> 
<rule name="Add query string param 15" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^sub2" />
    <add input="{QUERY_STRING}" pattern="qs=15" negate="true" />
    <add input="&amp;{QUERY_STRING}" pattern="^(&amp;.+)|^&amp;$" />
  </conditions>
  <action type="Redirect" url="http://{HTTP_HOST}/{R:0}?qs=15{C:1}" appendQueryString="false"  />
</rule> 

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

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