Wednesday, 10 August 2016

IIS; 3 ways to redirect

Using IIS 8.5 there are three was to re-direct traffic to another location. In my case I needed to redirect traffic from one IIS site to another. Requirements for redirection are not always the same, and sometimes an individual page or file may need to be redirected. In all cases a variation of one of the examples below should be sufficient to redirect traffic in the way you need.

Method 1 HTTP ReDirect

Redirect traffic using the HTTP Redirection feature.

Http Redirect should be the most simple method of redirecting traffic to another location, however in my case it caused the most confusion and led me to explore the other methods listed below. 

The Http Redirection feature must be installed before this method can be used.

When adding a redirect rule using this method, the system.webServer section of the web.config file for the site is updated with the redirection rule. In my case I wanted to redirect traffic from site1 to site2. However site1 and site2 were both sharing the same physical folder and site content. This meant that changing the HTTP Redirect setting for one changed it for both, leaving me with a situation where neither site was working.

The extract below shows the system.webServer section of the web.config file updated with the <httpRedirect> setting.

    <location path="www.mysite.co.uk">
        <system.webServer>
            <security>
                <authentication>
                    <anonymousAuthentication userName="" />
                </authentication>
                <access sslFlags="Ssl" />
            </security>
   <httpRedirect enabled="true" destination="https://www.myothersite.co.uk" exactDestination="true" httpResponseStatus="Permanent" />
        </system.webServer>
    </location

To get around this problem I moved the <httpRedirect> tag into the applicationHost.config file, (I suggest making a backup of applicationHost.config before making any changes)

This is how I did it:

1. Open this file:
C:\Windows\System32\inetsrv\config\applicationHost.config

2. Search for www.mysite.co.uk

3. Scroll through the results until you see the result in the <location> element. 

4. Add the httpRedirect rule inside the system.webServer tags.

It will probably look something like this:

<location path="www.mysite.co.uk">
        <system.webServer>
            <security>
                <authentication>
                    <anonymousAuthentication userName="" />
                </authentication>
                <access sslFlags="Ssl" />
            </security>
   <httpRedirect enabled="true" destination="https://www.myothersite.co.uk" exactDestination="true" httpResponseStatus="Permanent" />
        </system.webServer>
</location>

You can then delete httpRedirect out of the web.config file.


Redirecting is much simpler using the IIS Management Console, and can be done like this:

Select whatever it is you want to redirect, then double click HTTP Redirect (make sure Features View is enabled)




You can then enable redirection by ticking the "Redirect requests to this destination" box and clicking Apply. 






Method 2 Bindings

Redirect traffic by changing the site bindings

The site Bindings tell IIS which site should receive the traffic it is to process. A single site can have many different bindings, and as long as traffic is routed correctly to the server it will be processed by IIS and passed to the relevant site.

If I want to redirect traffic for site1 to site2, I can add the binding for site1 to site2. See below for an example



In this example the site will receive traffic for both www.mysite.co.uk and www.myothersite.co.uk (as long as the public dns record exists and is routing to the server).

One thing to note is that each site binding must be unique. Two identical bindings on different sites will cause an error in IIS meaning only one of the sites can be started.

For this example it is not necessary for the other sites to exist in IIS, only the binding is required for the data to reach the site.



Method 3 URL ReWrite

Redirect traffic using the URL Rewrite module.

This is the most difficult and complicated method of redirecting traffic, however it is also the most flexible.

There are a lot of resources available for the URL Rewrite module, but I found this page to be the most useful.

URL Rewrite can be be used to accomplish multiple different tasks, so to keep things simple I will only describe how I used it to redirect traffic from one site to another.

Step 1
Install URL Rewrite module

Once installed, the URL Rewrite module is available from the IIS Management Console in Features View.



Step 2
Create a Rewrite rule

There are two types of scope:

Global - will edit the applicationHost.config file using the <rewrite> tag, and will affect all traffic to all sites
Rule - can be defined on any other level. From the documentation "This type of rule can be added on any configuration level by using Web.config files or by using <location> tags within ApplicationHost.config or Web.config files."

I used a global rule, and added a condition that matches the site(s) I wanted to redirect.

1. Open the URL Rewrite module

2. Select Add Rule(s)... > Blank Rule

3. Give the rule a name

4. Match URL

This section is used to match anything after the host part of the site address, as shown below

Http://www.mysite.co.uk/MatchURL

I want to redirect every request for the site, so I'm matching everything. The setting for this is:

Requested URL = Matches the Pattern
Using = Regular Expressions
Pattern = (.*)

The data matched here can be used for rewriting and redirecting using a process known as back-references. Back-references to rule patterns are identified by {R:N} where N is from 0 to 9. The match here would be represented by {R:0}. If the regular expression produced multiple results, they would be represented by {R:1} and so on.

5. Conditions

Conditions are optional. They allow filtering based on the host part of the site address, so I am using conditions to identify the parts of the address to be redirected.

Logical grouping = Match All/Match Any (depending on requirements)

Select Add

Condition input = {HTTP_HOST}
Check if input string: = Matches the Pattern
Pattern: = ^(www\.)(.*)$
Ignor case = ticked

Clicking Test Pattern allows us to test the regular expression and discover how the back references will be used.



Back-references to condition patterns are identified by {C:N} where N is from 0 to 9. This can be seen in the image above. To redirect the site to new.mysite.co.uk we can now create a redirect action using http://new.{C:2}

To redirect the site to www.myothersite.co.uk we can create a redirect action using http://{C:1}myothersite.co.uk

To filter the rule to apply to certain sites, we can create additional conditions




The condition above will only apply to www.mysite.co.uk

The combination of regular expression matching and back references provide an unlimited number of combinations for matching a redirecting.




   
References http://www.andrewwestgarth.co.uk/blog/post/2008/07/31/creating-http-redirects-in-iis7-on-virtual-directories-like-iis6.aspx
https://www.stokia.com/support/misc/web-config-response-redirect.aspx
https://www.iis.net/configreference/system.webserver/httpredirect

No comments:

Post a Comment