Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS URL rewrite - Add query string depending on url

Tags:

.net

asp.net

iis

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> 
like image 368
benjamin54 Avatar asked Nov 23 '25 08:11

benjamin54


1 Answers

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>
like image 101
nanestev Avatar answered Nov 24 '25 22:11

nanestev