0
votes

I have a piece of JavaScript code, which when a user goes to mypage without a query string, I redirect to the same page with a specific query string.

I thought it's better to do this with an IIS URL rewrite rule, like this:

<rules>
  <rule name="mypage" stopProcessing="true">
    <match url="/mypage/*$" />    
    <action type="Redirect" url="/mypage/?category=shopping" appendQueryString="true" />
  </rule>
</rules>

It's simply not working.

I tested the regex with IIS tools and it is fine, also the rewrite rule is picked by IIS, so there shouldn't be a problem there. I set appendQueryString to true and false, just in case. but it didn't work in either case.

Also, I tried both "Rewrite" and "Redirect" action types, just in case. Didn't work.

Could you please help? thanks.

1
I would like to see the new query string in the URL, so "Redirect" action type is preferred.Siavash Mortazavi

1 Answers

1
votes

You need to remove the first slash in the match url. You also need to check for the querystring already being there otherwise it will go into an infinite redirect loop. You will not have this issue if you do rewrite

   <rule name="mypage" stopProcessing="true">
      <match url="^mypage/*$" />
      <action type="Redirect" url="/mypage/?category=shopping" appendQueryString="true" />
      <conditions>
        <add input="{QUERY_STRING}" pattern="category=shopping" negate="true" />
      </conditions>
    </rule>