Saturday, November 7, 2009

IIS Makes Website Redirection Easy

I have found many uses for the redirection functionality that is built into IIS over the years and I am surprised by how many times I come across people using redirect files or other methods to redirect people to new sites or to redirect people to SSL.

Microsoft has some good information here on how to use redirection in IIS. However, I thought I would give some real world examples and provide some screenshots. You will also want to check out this reference to see all of the available parameters. OK, let's get started.

Redirect to SSL

I use IIS redirection the most to redirect regular web traffic to SSL. Here is how:

Example: We want to redirect http://mail.domain.com to SSL. [Site] will be used as a placeholder for whatever you want to call the website in IIS.

Step 1: Set up website called "Redirect [Site] to SSL" (This website will only run on port 80 and will not be assigned a server certificate. You may have to add a host header or assign a specific IP to this site if you are hosting multiple sites.)

Step 2: Now, go to the properties of the site you just created, select the 'Home Directory' tab, select the 'A Redirection to a URL' radio button, under 'Redirect to:' enter 'https://mail.domain.com'. For this particular example, you can ignore the check boxes below. However, you may want to use one based on your need and security requirements so do some research.



Step 3: Set up website called [Site] and assign it a certificate so that it will accept SSL connections. (You will either need to add a fake host header, I use 'asdf', or change the TCP port to another number, e.g. 8080 or 1234, so that the sites will not interfere with each other. TCP port is not needed on this site since we want the 'Redirect [Site] to SSL' to respond for HTTP (port 80) traffic.)

Done.

Redirect to SSL Exchange 2003 & 2007

Step 1: Follow directions for 'Redirect to SSL', but instead of entering 'https://mail.domain.com' in step 2 enter 'https://mail.domain.com/exchange' (Exchange 2003) or 'https://mail.domain.com/owa' (Exchange 2007).

Problem: If someone enters 'https://mail.domain.com', no redirection will take place because the traffic is not hitting the redirect site.

Solution: Go to the 'Properties' of the root of your [Site] (the one running SSL) and select the 'Home Directory' tab. Select the 'A Redirection to a URL' radio button, under 'Redirect to:' enter 'https://mail.domain.com/exchange' (Exchange 2003) or 'https://mail.domain.com/owa' (Exchange 2007). Then, check the check box 'A directory below URL entered'. (If you do not check this box, it will apply redirection to all of the sub-directories also. You do not want that.)



Done.

Redirect and Preserve Suffix and Parameters (Query String) - Redirection Parameters

Example: You want to redirect 'http://mail.domain.com' and 'https://mail.domain.com' to 'https://mail.newdomain.com', but you want to make sure that 'http://mail.domain.com/default.aspx?User=dhazar' or 'https://mail.domain.com/default.aspx?User=dhazar' redirect to 'https://mail.newdomain.com/default.aspx?User=dhazar'. You want to make sure that the redirection preserves the suffix and the parameters

How: Assuming the sites are already set up go to 'Properties' of the site that responds for 'http://mail.domain.com' and 'https://mail.domain.com', select 'Home Directory' tab, select the 'A Redirection to a URL' radio button, under 'Redirect to:' enter 'https://mail.newdomain.com$S$Q', check 'The exact URL entered above'. When using 'Redirection Parameters', you may (not always) need to check the check box 'The exact URL entered above'. In this example, the box must be checked. $S and $Q are 'Redirection Parameters'.



I won't provide any more examples here, but see the 'Redirect Reference' here to find out more. There are a few more useful parameters and it explains how to use wildcards for more advanced redirection.

5 comments:

  1. Your blog helped me rectify an issue I've been working on intermittently for 2 months.

    Many thanks

    ReplyDelete
  2. you are the champion. After going to production we started getting calls from bookmarked users as it redirected to the base URL and we were able to fix it without loss of time in research and test.

    ReplyDelete
  3. Happy you found what you needed without too much trouble!

    ReplyDelete
  4. What is both domains point to the same site in IIS but want to redirect from one to another.
    Ex:
    MyDomain.Org should do a permanent redirect to MyDomain.com , but both pointing to same site.

    ReplyDelete