Forum Discussion

Samir_Jha_52506's avatar
Samir_Jha_52506
Icon for Noctilucent rankNoctilucent
Oct 15, 2015

URL Redirection request.

Hi All,

I wanted to write irule with help of "Class-object" but need your help to grep ID value (SAV603,SAV644, SAV651,etc) in URI. SO that based on object i can forward request to respective direction.

Redirect the request to http://go.pqr.com/elib for below urls & rest will work as usual.

http://uattest-XYZ.com/bin/enterprise?siteName=a&No=SAV603 https://uattest-XYZ.com/bin/enterprise?siteName=a&No=SAV644 http://uattest-XYZ.com/bin/enterprise?siteName=a&No=SAV651

I have written irule with help of Switch but in future if user wanted to redirect more URL, Class option will be more helpful. So i wanted to redirect URL based on ID((SAV603,SAV644, SAV651,etc)

Switch Irule
            when HTTP_REQUEST {
            switch -glob [string tolower [HTTP::path]] {
            "/bin/enterprise?siteName=a&No=SAV603" -
             "/bin/enterprise?siteName=a&No=DP6012" -
             "/bin/enterprise?siteName=a&No=SA1005" { HTTP::redirect "http://go.pqr.com/elib" }

                     default {}
                            }
            }

4 Replies

  • Hi,

    HTTP::path of these URLs is /bin/enterprise and not /bin/enterprise?siteName=a&No=SAV603

    /bin/enterprise?siteName=a&No=SAV603 is stored in the variable HTTP::uri

    ? character with the -glob option will match any character and not only ?

    there is also another issue in the irule a lower case URI cannot be compared with a string containing uppercase characters

    when HTTP_REQUEST {
        switch [HTTP::uri] {
            "/bin/enterprise?siteName=a&No=SAV603" -
            "/bin/enterprise?siteName=a&No=DP6012" -
            "/bin/enterprise?siteName=a&No=SA1005" { HTTP::redirect "http://go.pqr.com/elib" }
            default {}
        }
    }
    
  • Arie's avatar
    Arie
    Icon for Altostratus rankAltostratus

    To use a class:

        if { ([class match [string tolower [HTTP::query]] contains class_NoQueryStringValuesToRedirect]) } {
    
            HTTP::response 301 Location "http://go.pqr.com/elib"
    
        }
    

    The values in the class would be as follows (lower case!):

    no=sav603
    no=dp6012
    [...]
    

    You could get more fancy and extract the value instead of comparing to the name+value, but that's probably not necessary here.

    Also, note that I used a 301 instead of a 302. The benefit is that it gets cached by clients (saving requests). However, if the destination is likely to change you'll want to use a 302 (which is what

    HTTP::redirect
    generates).

  • you can extract value of No parameter:

    when HTTP_REQUEST {
        set ParamsiteName [URI::query [HTTP::uri] siteName]
        if {([string tolower [HTTP::path]] equals "/bin/enterprise") && ($ParamsiteName equals "a")} {
            set ParamNo [URI::query [HTTP::uri] No]
            switch $ParamNo {
                "SAV603" -
                "DP6012" -
                "SA1005" { HTTP::redirect "http://go.pqr.com/elib" }
                default {}
            }
        }
    }
    

    or with class as requested:

    when HTTP_REQUEST {
        set ParamsiteName [URI::query [HTTP::uri] siteName]
        if {([string tolower [HTTP::path]] equals "/bin/enterprise") && ($ParamsiteName equals "a")} {
            set ParamNo [URI::query [HTTP::uri] No]
            if { ([class match $ParamNo contains class_NoQueryStringValuesToRedirect]) } {
                HTTP::redirect "http://go.pqr.com/elib" 
            }
        }
    }
    
  • I think you misunderstand about support of class in version 11.4...

     

    HTTPclass are not supported anymore in 11.4 version and are replaced by Local Traffic Policies.

     

    class command in irule refer to Data group List in WebUI which is still supported.

     

    So the irules provided by Arie and I will work after upgrades.