Forum Discussion

Chris_Day_10331's avatar
Chris_Day_10331
Icon for Nimbostratus rankNimbostratus
Jan 04, 2006

Can this redirect rule be optimized?

Hey guys,

I created a pretty basic/lame redirect rule and I thought I'd post it to see if anyone had any recommendations on how to optimize it. I'll paste the goods first since I think it basically speaks for itself, then explain why I'm hoping to optimize:


when HTTP_REQUEST {
   switch -glob [HTTP::host] {
      "*domain.ca" {
         log local0. "Detected domain.ca URL"
         HTTP::redirect "http://domain.myasp.com[HTTP::uri]"
      }
      "*domain.com" {
         log local0. "Detected domain.com URL"
         HTTP::redirect "http://domain.myasp.com[HTTP::uri]"
      }
      default {
         log local0. "Using default pool"
         use pool http-pool-myasp
      }
   }
}

This list could get a bit longer, and it could be used across multiple VSs (HTTP & HTTPS). I guess what I'm wondering is if this is a good way to go or if perhaps there is a more efficient way to put this together?

As it stands now I am having to create a different iRule for each "set" of domains. I believe I could use a data group to simplify the rule but I am not sure how that would look nor if there would be performance implications of settings up several (dozens?) of mappings within one data group and applying this single iRule to all VSs. I am also not sure how the wildcard would play into this when combined with data groups. Please keep in mind I have extremely novice programming experience but can usually walk my way through a rule.

Cheers, and thanks!

Chris

1 Reply

  • Using a switch is one of the most optimal ways to do this. But if, as you said, you foresee manageability issues with the large number of domains across multiple virtuals, then you could do it with data groups. Here's an alternate implementation using a string datagroup with the domain mapping contained in the data group.

    class domain_mappings {
      "domain.ca domain.myasp.com"
      "domain.com domain1.myasp.com"
      "domain.biz domain2.myasp.com"
      "joe.com domain3.joe.com"
      "fred.us domain4.fred.com"
    }
    when HTTP_REQUEST {
       Extract the domain portion of the host
      set valid_domain [regexp {\.([\w]+)\.([\w]+)$} [HTTP::host] domain company ext]
      if { $valid_domain }
      {
         Look for match in the domain_mappings data group
        set new_domain [findclass "$company.$ext" $::domain_mappings " "]
        if { "" ne $new_domain } {
           Issue redirect
          HTTP::redirect http://$new_domain[HTTP::uri]
        }
      }
    }

    There's probably some more optimal way to extract only the last two tokens from the host aside from a regexp but this should account for any type of host you specify (www.foo.com, www1.www.foo.com, etc).

    So, with this option, you would either maintain a single master domain_mappings list and apply the same iRule across all of your virtuals, or split out the domain_mappings data group into smaller more manageable ones. Your choice.

    *disclaimer: This is probably lower performance than your switch option, but it is a much simpler version for when you scale to a larger format.

    -Joe