0

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. Commented Feb 3, 2017 at 17:49

1 Answer 1

1

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

2 Comments

Thanks Jeroen. The first slash is required. your regex can match "www.mysite.com/this-is-mypage" which is not desired in my case. Can you be more specific about the second part of your answer? How can I "check for the querystring already being there"?
If you want to avoid matching "www.mysite.com/this-is-mypage" then you need to use a ^ so it starts matching at the start of URL. You can add the querystring as a condition and negate it so it matches everything but the querystring to avoid a redirect loop. I updated my original answer with the full rule.

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.