How to redirect using F5 iRules with a variable in the URL

18.8k Views Asked by At

Hi I'm new to F5 iRule.

I'm trying to redirect https://website1.com/userid=1234 to https://website2.com/userid=1234

such that whatever value the userid may have will be carried over after redirection.

I'm thinking userid value should be set to a variable. Can someone share what code to use? Thanks!

So https://website1.com/userid=8888 should go to https://website2.com/userid=8888 and so on.

1

There are 1 best solutions below

0
On

You shouldn't need a variable if that pattern is consistent. A simple rule would be:

when HTTP_REQUEST {
  if { [HTTP::host] eq "website1.com" } {
    HTTP::redirect https://websitesite2.com[HTTP::uri]
  }
}

However, if you are on v11.4+, you really should use a local traffic policy for this as it is more performant as a built-in feature of TMOS.

ltm policy sample_site_redirect {
    controls { forwarding }
    last-modified 2018-12-20:09:33:02
    requires { http }
    rules {
        full_uri_redirect {
            actions {
                0 {
                    http-reply
                    redirect
                    location tcl:https://website2.com[HTTP::uri]
                }
            }
            conditions {
                0 {
                    http-host
                    host
                    values { website1.com }
                }
            }
        }
    }
    status published
    strategy first-match
}

if all traffic to the virtual server this rule or policy is attached to is intended for website1 only, you can eliminate those conditions. I didn't want to assume. If it's only the URI starting with /user= that you want to match, and redirect on, you can do that this way:

when HTTP_REQUEST {
  if { ([HTTP::host] eq "website1.com") && ([string tolower [HTTP::uri]] starts_with "/user=") } {
    HTTP::redirect https://website2.com[HTTP::uri]
  }
}