cancel
Showing results for 
Search instead for 
Did you mean: 

iRule with multiple 301 redirects only redirecting main URI

Walter_WorkAcct
Altostratus
Altostratus

I am new to the F5 and working on an irule to 301 permanently redirect traffic from links to our old site to new locations on the new site. The issue is that only the default portion of the code is working. It isn't picking up on the switch statements to redirect to other areas. Can anyone point me in the right direction on how to troubleshoot?

 

Example code:

when HTTP_REQUEST {   if { [HTTP::uri] contains "oldsite.ournewsite.net" } {     switch -glob [HTTP::uri] {       "*/foo" { HTTP::respond 301 "Location" "https://ournewsite.net/oldsitestuff/foo"}   "*/bar" { HTTP::respond 301 "Location" "https://ournewsite.net/oldsitestuff/bar"}   default { HTTP::respond 301 "Location" "https://ournewsite.net/oldsitestuff"}     }   }  }
5 REPLIES 5

Hi Walter_WorkAcct,

 

You should use [HTTP::host] to compare "oldsite.ournewsite.net"

when HTTP_REQUEST { if { [HTTP::host] equals "oldsite.ournewsite.net" } { switch -glob [HTTP::uri] { "*/foo" { HTTP::respond 301 Location https://ournewsite.net/oldsitestuff/foo } "*/bar" { HTTP::respond 301 Location https://ournewsite.net/oldsitestuff/bar } default { HTTP::respond 301 Location https://ournewsite.net/oldsitestuff } } } }

 

Walter_WorkAcct
Altostratus
Altostratus

Enes Afsin Al, thank you for the response and I do appreciate it but even changing that comparison only works on the default. For example, oldsite.ournewsite.net/foo goes to https://ournewsite.net/oldsitestuff instead of https://ournewsite.net/oldsitestuff/foo

when HTTP_REQUEST { log local0. "oldsitelog1 | host = [HTTP::host] | uri = [HTTP::uri] | client_ip = [IP::client_addr]" if { [HTTP::host] equals "oldsite.ournewsite.net" } { switch -glob [HTTP::uri] { "*/foo" { log local0. "oldsitelog2 foo | uri = [HTTP::uri] | client_ip = [IP::client_addr]" HTTP::respond 301 Location https://ournewsite.net/oldsitestuff/foo return } "*/bar" { log local0. "oldsitelog3 bar | uri = [HTTP::uri] | client_ip = [IP::client_addr]" HTTP::respond 301 Location https://ournewsite.net/oldsitestuff/bar return } default { log local0. "oldsitelog4 default | uri = [HTTP::uri] | client_ip = [IP::client_addr]" HTTP::respond 301 Location https://ournewsite.net/oldsitestuff return } } } }

Can you investigate ltm logs?

tail -f /var/log/ltm | grep oldsitelog

 

Hi

Looking at the iRule I'd check the position of the wildcard. Just reading it looks like it would only match URI's ending with "/foo" but I'd assume that you'd want to match where the URI starts with "/foo"

SanjayP
MVP
MVP

is your old site VIP only being used for HOST "oldsite.ournewsite.net"? In other words, Is there any other FQDN that also maps/shares same VIP address and shouldn't be redirected to the new site?

 

If this is standalone VIP for host oldsite.ournewsite.net, then you can use switch statement for URI and no need to specifically check for old site Host. iRule would be below

 

when HTTP_REQUEST { switch -glob [string tolower [HTTP::uri]] { "/foo*" { HTTP::respond 301 "Location" "https://ournewsite.net/oldsitestuff/foo" } "/bar*" { HTTP::respond 301 "Location" "https://ournewsite.net/oldsitestuff/bar" } default { HTTP::respond 301 "Location" "https://ournewsite.net/oldsitestuff" } } }

In case, your old site VIP also host some other hosts/fqdn behind it, then you can use something like below

 

when HTTP_REQUEST { switch -glob [string tolower [HTTP::host]] { "oldsite.ournewsite.net" { if { [string tolower [HTTP::uri]] starts_with "/foo" } { HTTP::respond 301 "Location" "https://ournewsite.net/oldsitestuff/foo" } elseif { [string tolower [HTTP::uri]] starts_with "/bar" } { HTTP::respond 301 "Location" "https://ournewsite.net/oldsitestuff/bar" else { HTTP::respond 301 "Location" "https://ournewsite.net/oldsitestuff" } } } default { return } } }