How to preserve query parameters using IIS HTTP wildcard redirects?

1.8k Views Asked by At

I'm trying to configure redirects to prepare the switch from an ASP to an MVC website. The IIS wildcard redirects are working fine, but I cannot figure out how to preserve the query parameters.

Example:

<system.webServer>
  <httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Permanent">
    <add wildcard="/EmailAddress-Verify.aspx" destination="https://foo.com/account/email-verify$Q" />
  </httpRedirect>
</system.webServer>

I read that $Q will preserve the query params but it's not working. Google could not find anything on that issue.. is there anything I'm missing here?

Thanks!

2

There are 2 best solutions below

3
On

Y̶o̶u̶ ̶h̶a̶v̶e̶ ̶e̶x̶a̶c̶t̶D̶e̶s̶t̶i̶n̶a̶t̶i̶o̶n̶=̶"̶t̶r̶u̶e̶"̶.̶ However, more importantly, when you want to have more granular control with redirects, you're much better off using the URL Rewrite module. Your bit would be something like the following (not tested and quickly written up):

<rewrite>
  <rules>
    <rule name="Email Address Verify Redirect" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
         <add input="{REQUEST_URI}" pattern="^EmailAddress-Verify.aspx(.*)$" />
      </conditions>
      <action type="Redirect" redirectType="Permanent" url="https://foo.com/account/email-verify{R:1}" />
    </rule>
 ...
 </rewrites>

Oh, now that I think about it, you could probably just do a straight up pattern without the following wildcard and just appendQueryString="True" in the action.

0
On

So this configuration was working perfectly! I just had to clear my browser cache since the redirection is Permanent and the browser 'remembered' my previous configuration while I was playing with the config file. My bad!