Forum Discussion

Monty_152734's avatar
Monty_152734
Icon for Nimbostratus rankNimbostratus
Apr 04, 2017

Redirect on multiple conditions URI & Query

Hi Guys, This is my first time at DevCentral.

 

I need help. The requirement are when f5 see URI ( https://abc.xyz.com/shop/window.yes) and query (car=true) then redirect to url (https://abc.xyz.com/shop/door.yes?car=ture) if query car=false do nothing.

 

means if some try https://abc.xyz.com/shop/window.yes?car=ture url should redirect to https://abc.xyz.com/shop/door.yes?car=ture

 

here is a irule what I am using but it is not working, can someone tell me what is wrong.

 

when HTTP_REQUEST { if { [string tolower [URI::query [HTTP::uri] "cart" ] ] equals "true" } { switch -glob [string tolower [HTTP::path]] { "/shop/window.yes*" { HTTP::redirect "[HTTP::host]/shop/door.yes?[HTTP::query]" } } } }

 

3 Replies

  • JG's avatar
    JG
    Icon for Cumulonimbus rankCumulonimbus

    Try this one:

    when HTTP_REQUEST {
        log local0. "URI in: [HTTP::uri]"
        if { ([string tolower [HTTP::path]] starts_with "/shop/window.yes") and \
        ([string tolower [URI::query [HTTP::uri]]] equals "car=false")} {
            log local0. "Matched: Path: [HTTP::path]; Query: [URI::query [HTTP::uri]]"
            HTTP::redirect "/shop/door.yes?car=true"
        }
        if { [string tolower [HTTP::path]] starts_with "/shop/" } {
            log local0. "Not matched: Path: [HTTP::path]; Query: [URI::query [HTTP::uri]]"
            pool example-pool
            persist none
        }
    }
    

    [Edited to include logging.]

  • Hi,

    I will ask for some issue happens to everyone :

    when you use

    [string tolower [HTTP::path]]
    , is the string you defined lowercase?

    for example, the following condition will never match:

    [string tolower [URI::query [HTTP::uri] "cart" ] ] equals "True"
    
  • Hi,

    Are PATH and QUERY in irule real ones?

    In your examples and irules, you wrote:

    • cart=true
    • car=true
    • car=ture

    the following irule may work for car=true

    when HTTP_REQUEST {
        switch -glob -- [string tolower [HTTP::path]] {
            "/shop/window.yes" {
                if { [string tolower [URI::query [HTTP::uri] "car" ] ] equals "true" } {
                    HTTP::redirect "/shop/door.yes?[HTTP::query]"
                }
            }
            "/shop/* {
                pool example-pool
                persist none
            }
        }
    }