Capture part of URI and do not redirect.

I am doing redirection for path like this:

/abc (and whatever parameters after /abc) is being redirected to /xyz

when HTTP_REQUEST {
if { [HTTP::host] contains “example.com” }{
elseif { [HTTP::uri] starts_with “/abc” } {
HTTP::redirect /xyz
}

My new requirement is:

If the uri is /abc (and whatever parameters after /abc, eg: /abc?=un), it should redirct to /xyz
If the uri is /abc-def (and whatever parameters after /abc-def, eg: /abc-def?=un, it should stay on same URI /abc-def

Could you please let me know how this can be acheived? Thanks

I think you can just replace:

[HTTP::uri] starts_with "/abc"

with:

[HTTP::path] equals "/abc"

I think you need to use (not starts_with) operator for your logic.

In the below code sample, if the request equals to “abc” or not starts with “abc-def”, it will be redirected to /xyz.

Therefore,

"If the uri is /abc (and whatever parameters after /abc, eg: /abc?=un), it should redirct to /xyz
If the uri is /abc-def (and whatever parameters after /abc-def, eg: /abc-def?=un, it should stay on same URI /abc-def"

  • /abc will be redirected to /xyz
  • /abc?=un will be redirected to /xyz (because it is not starts with “abc-def”
  • /abc-def will stay on same because it will not going into second if statement.

when HTTP_REQUEST {
    if {[HTTP::host] eq "www.example.com"}{
        if{(([HTTP::uri] eq "abc") or !([HTTP::uri] starts_with "abc-def"))  }{
        HTTP::redirect "www.example.com/xyz"
    }
    }
}

i suggest you use gui based local traffic policy rather than irules scripting for this traffic modification

https://community.f5.com/t5/technical-articles/to-irule-or-not-to-irule-introduction-to-local-traffic-policies/ta-p/279536

Thank you all for your help.
I write this irule and solve my problem.

when HTTP_REQUEST {
if { [HTTP::host] contains “example.com” }{
elseif { [HTTP::uri] starts_with “/abc” and not ([HTTP::uri] contains “abc-def”) } {
HTTP::redirect /xyz

This is what happening.

example.com/abc → redirecting to example.com/xyz
example.com/abc?=un → redirecting to example.com/xyz

example.com/abc-def → It stays as it is. This is my requirement.
example.com/abc-def?=un → It stays as it is. This is okay for me now.

I was looking if there is anyway example.com/abc-def?=un can also be redirected to example.com/abc-def by modifying the irule above.

when HTTP_REQUEST {
    set HOST [string tolower [HTTP::host]]
    set URI [HTTP::uri]

    if { $HOST equals "example.com" } {
        if { $URI equals "/abc-def?=un" } {
            HTTP::redirect "/abc-def"
            return
        } elseif { $URI starts_with "/abc" && !($URI contains "abc-def") } {
            HTTP::redirect "/xyz"
        }
    }
}

Thanks. Make sense.