Tuesday, August 14, 2012

What Microsoft Didn't Tell You of IIS Redirects

It was another day with IIS (gulp!) and bad times came upon as usual. This time it was about redirecting a site request to another site, but still maintaining rest of the URL unchanged.

Let's say you want to run two sites in the same server:
 oldsite.com
 newsite.com 
If any request to oldsite.com should be redirected to newsite.com, it's quite easy to do in IIS. Just create a site bound to oldsite.com and in the redirect setup dialog, define the redirect URL as http://newsite.com.

But because the target URL is a static entry, any request to oldsite.com, no matter what the URL parameters are, will get plainly redirected to newsite.com. If a user requests for oldsite.com/product.php?sku=12345, IIS simply takes to newsite.com, showing your user the home page instead of the product page. Too bad!

IIS displays no obvious option for preserving request queries and pass them on to the target URL. To achieve this, just specify the target URL as:
 http://newsite.com$S$Q 
Yes, that's all it takes! The $S says any path must be applied to the new URL as well and $Q says that any parameter variables must be passed to the new URL.

So, requests to
 http://oldsite.com/product.php?sku=12345 
will get redirected to
 http://newsite.com/product.php?sku=12345 
Why there's no visible option for this in the redirect menu? You know.. we are dealing with Microsoft.