HTTP>HTTPS Redirect to different domain

Hi all, this should be pretty simple…hopefully.

I have a simple HTTP>HTTPS redirect in place for a domain --> HTTP::redirect “https://[HTTP::host][HTTP::uri]”

We have several subdomains (~15) xxx.domainA.com, yyy.domainA.com, etc. What we would like to do now is redirect those same sub domains to a new domain:

http://xxx.domainA.com to https://xxx.domainB.com

http://yyy.domainA.com to https://yyy.domainB.com

http://zzz.domainA.com to https://zzz.domainB.com

Is there any easier way to do this besides using a switch command for ever sub domain? I.E.

when HTTP_REQUEST {

switch [HTTP::host] {

xxx.domainA.com {

HTTP::redirect “https://xxx.domainB.com”

}

yyy.domainA.com {

HTTP::redirect “https://yyy.domainB.com”

}

etc…

Thanks in advance for the help!

I’d create a data group containing all the domains, check for a match, and if a match exists, use string to insert the value. Otherwise, you could use class search - value if you’re on a newer version of code. What version are you on?

Thanks for the response Chris, I am running 10.2.0 HF2.

Could you provide an example of the class search as it relates to what I am trying to do? I am not familiar with that functionality. Thanks!!

Sure.

You’d have a string type data group called something like “domainredirects.” In that group, you’d have a string like “xxx.domaina.com” with a value like “xxx.domainb.com.” Then, your rule would look something like this:

when HTTP_REQUEST {
   if {  [class match [HTTP::host] eq domainredirects] } {
         HTTP::redirect "https://[class search - value  domainredirects eq [HTTP::host]]" }
}

Otherwise, if your scheme is as simple as xxx.domaina.com > xxx.domainb.com, then a string command would be more efficient. All depends on how many characters you’re changing I suppose.

Nice suggestion Chris. You could save the second lookup by returning the name and value in the first lookup. Here’s an untested example:

when HTTP_REQUEST {
   set lookup [class search -element domainredirects equals [string tolower [HTTP::host]]]
   if {  [llength $lookup] == 2 } {
         HTTP::redirect "https://[lindex $lookup 2][HTTP::uri]"
   }
}

Aaron

@Aaron - Is there a reason for returning the element rather than the value in the search? You could save the lindex lookup if you returned the value directly. A bit nit-picky I suppose but I’m more curious if there’s some reason you’re doing it that I’m not seeing rather than trying to nit pick optimizations. :wink:

Colin

Thanks guys, I implemented Chris’ solution and it worked like a charm, but when I tired Aaron’s it did not work for some reason :frowning:

I like nit-picking to get the most efficient solution :). I was being a dolt and thinking of another recent conversation where we needed the matched name and value for later logic. In this case, you just need the matched value, so something like this should work:

when HTTP_REQUEST {
   set host [class search -value domainredirects equals [string tolower [HTTP::host]]]
   if { $host ne "" } {
         HTTP::redirect "https://$host[HTTP::uri]"
   }
}

Aaron

if you always know that you want to move domainA.com to domainB.com, you could do something more generic like this

when HTTP_REQUEST {
  if { [HTTP::host] ends_with "domainA.com" } {
    HTTP::redirect "https://[getfield [HTTP::host] "." 1].domainB.com[HTTP::uri]"
  }
}

That would redirect all requests to XXX.domainA.com to XXX.domainB.com retaining the URIs.

Of course, if you have more domains to match on, then the class lookup will likely be a better solution.

-Joe

Hi Joe

Your suggestion looks good.

Posted By Joe on 02/10/2011 01:44 PM

if you always know that you want to move domainA.com to domainB.com, you could do something more generic like this

when HTTP_REQUEST {
  if { [HTTP::host] ends_with "domainA.com" } {
    HTTP::redirect "https://[getfield [HTTP::host] "." 1].domainB.com[HTTP::uri]"
  }
}

That would redirect all requests to XXX.domainA.com to XXX.domainB.com retaining the URIs.

Of course, if you have more domains to match on, then the class lookup will likely be a better solution.

-Joe

Hi Joe

Your suggestion looks good. Does your iRule works the same if we are redirecting from .com to say .com.sg?

Sure, just add the domain suffix to the redirect:

when HTTP_REQUEST {
  if { [string tolower [HTTP::host]] ends_with "domaina.com" } {
    HTTP::redirect "https://[getfield [HTTP::host] "." 1].domainB.com.sg[HTTP::uri]"
  }
}

Aaron

What if we have multiple prefix to the domain. whill that be reditected too. For example if we have xxx.yyy.DomainA.com this does not get routed to xxx.yyy.DomainB.com with the above rule. Am i missing anythin here.

e.g.

[root@ve10:Active] config  b virtual bar list
virtual bar {
   destination 172.28.19.79:80
   ip protocol 6
   rules myrule
   profiles {
      http {}
      tcp {}
   }
}
[root@ve10:Active] config  b rule myrule list
rule myrule {
   when HTTP_REQUEST {
   set host [string tolower [HTTP::host]]
   if {$host ends_with "domaina.com"} {
      HTTP::redirect "https://[string map {"domaina.com" "domainb.com"} $host][HTTP::uri]"
    }
}
}

[root@ve10:Active] config  curl -I http://172.28.19.79/something -H "Host: www.domaina.com"
HTTP/1.0 302 Found
Location: https://www.domainb.com/something
Server: BigIP
Connection: Keep-Alive
Content-Length: 0

[root@ve10:Active] config  curl -I http://172.28.19.79/something -H "Host: xxx.yyy.domaina.com"
HTTP/1.0 302 Found
Location: https://xxx.yyy.domainb.com/something
Server: BigIP
Connection: Keep-Alive
Content-Length: 0