Friday, September 14, 2012

Multiple HTTP Redirects in IIS 7

PROBLEM: On one of our websites we recently moved from having a separate mobile site vs full site to just having a full site with "responsive design".  We initially just put in place a HTTP Redirect (301) in place via IIS 7 for the mobile site to redirect all mobile pages to the home page of the full site (hoping that people would update their bookmarks after getting redirected) - feedback said people didn't want to update their bookmarks so we needed a way to put in place multiple redirects.

SOLUTION: I couldn't see a way to put multiple HTTP Redirect rules in place easily via IIS 7 for a single site so I decided to use "URL Rewrite 2.0" for IIS 7, a module/extension for IIS provided by Microsoft that has to be downloaded and installed separately (watch out for the first gotcha!)
  1. To install download it from  http://www.iis.net/downloads/microsoft/url-rewrite which will install it via the Web Platform Installer... BUT... be aware, that despite a lack of warning it will:
    1. Stop all the websites on your server.
    2. Require a server reboot before you can use it
  2. After you've scrambled to reboot your server after seeing all your sites down then it's simply a matter of opening the "URL Rewrite" module you'll now see in IIS for your site and creating a number of redirect rules - here is some info: http://www.iis.net/learn/extensions/url-rewrite-module/using-the-url-rewrite-module but this is what I did
    • Requested URL = Matches Pattern
    • Using = Regular Expressions (man, these always suck if you haven't learnt them! A cheat sheet might help you: http://regexlib.com/CheatSheet.aspx ... I find trying to search for regular expressions in regexlib is pointless if it's a general concept you're looking for e.g. "find me all matches with a word but not having this word" vs "email validator")
    • Pattern: e.g. (?!.*timetable.*)^clubs/(.*)/(.*)$
      • Matches any URL that starts with clubs/ and doesn't contain the word timetable
    • Another example: ^clubs/(.*)/(.*)/timetable.*$
      • Matches any URL that starts with clubs/ and then has timetable at the end
    • Ignore case = ticked
    • Action Type: Redirect
    • Redirect URL: e.g. http://yoursite.com/clubs/{R:1}/{R:2}/ (see how you can grab anything from the regular expression that was in brackets (known as a back reference apparently) and use it in the redirect URL.
    • Another example: http://yoursite.com/clubs/{R:1}/{R:2}/timetable/
    • Append Query String = false (not required since we were using friendly urls without query strings anyway).
    • Redirect Type: Permanent (301).
  3. To ensure all other pages are redirected to the root of your homepage simply update your HTTP Redirect settings in IIS
    1. Redirect requests to this destination: Ticked
    2. http://yoursite.com/
    3. Redirect all requests to exact destination (instead of relative to destination).
    4. Status code: Permanent (301).
I'm sure there are better ways out there and would love to hear them.  Otherwise hopefully this helps someone else - at the very least hopefully someone will read the warning about the installation of URL Rewrite causing your sites to stop and the server requiring a reboot!

No comments:

Post a Comment