3

I have added multiple http bindings to my site like:

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.

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. 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. 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> 
1

2 Answers 2

3
+50

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>
Sign up to request clarification or add additional context in comments.

Comments

2

You need to use a HTTP_HOST condition to check for the subdomain like that:

<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> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.