Skip to content

Handling 301 and 302 redirects with IIS 7 URL Rewrite

We’ve got a couple sites running via proxies setup in IIS – this blog being one example.  Setting up a reverse proxy using IIS, the URL Rewrite module and Application Request Routing (ARR) is pretty straightforward.  However, what happens if the backend server does a redirect?  That response is going to make its way to the user’s browser with a Location header of where to redirect to and the browser will perform a redirect.  The problem is that the Location header is not going to have been updated by the proxy and your user will now be redirected to your backend server.

To stop this from happening we need to rewrite the location header.  This is far from obvious if you’re just clicking around the URL Rewrite dialogs, so here’s how!

Modifying Location Header with IIS URL Rewrite

  • Go to the URL Rewrite feature for your site and click Add Rule(s)…
  • Select <Create New Precondition…> from the Precondition drop-down.
  • Click Add in the dialog that appears
  • Enter {RESPONSE_STATUS} in the Condition input field and 3[0-9][0-9] in the pattern field.  Click OK.
  • Select Server Variable from the Matching scope drop-down.
  • Enter RESPONSE_Location as the Variable name.
  • In the Pattern field enter a regex to match the URLs your backend system is producing (e.g. http://local/page)
  • In the Action Value box enter the correct URL (e.g.
  • Click Apply and your done!

Pretty painless, but there are a few steps you’d never be able to guess!  Thanks to the Learn IIS blog!