Forum Discussion

veredgfbll's avatar
Jun 04, 2023

Help with irule using switch

I need an irule that does the following - If host name equals xxx.com and uri equals /yyy redirect to xxy.com.... but I need to switch between 6 different URIs - I want to use the SWITCH command bu...
  • Paulius's avatar
    Jun 04, 2023

    veredgfbll I believe what you're looking for is the following. Please keep in mind that you do not have to have the final else or the default action in the switch statement and these are just options if by chance you do have a default action you want to perform for the if statement if it doesn't match or the URI path for the single host match.

    when HTTP_REQUEST priority 500 {
    
        set URI [string tolower [HTTP::uri]]
    
        if { [HTTP::host] == "xxx.com" } {
            switch -- ${URI} {
                "/yyy" {
                    # can change http to https if you need to redirect to HTTPS instead
                    HTTP::redirect "http://xxy.com/"
                }
                "uri_2" {
                    # URI 2 action to execute
                }
                "uri_3" {
                    # URI 3 action to execute
                }
                "uri_4" {
                    # URI 4 action to execute
                }
                "uri_5" {
                    # URI 5 action to execute
                }
                "uri_6" {
                    # URI 6 action to execute
                }
                default {
                    # default action to perform if URI doesn't match
                }
            }
        } else {
            # action to perform if HTTP::host doesn't match
        }
    
    }

     

  • Paulius's avatar
    Paulius
    Jun 05, 2023

    veredgfbll The iRule you created can be configured as the following which you will notice the "default" match is completely removed since you don't want to perform any other action other than what the iRule and virtual server will already do if a match isn't found.

    when HTTP_REQUEST priority 500 {
    
        set URI [string tolower [HTTP::uri]]
    
        if { [HTTP::host] == "www.hostname.com" } {
            switch -- ${URI} {
                "/aaa" {
                    HTTP::respond 302 Location "https://www.test.com/aaa"
    				#log local0. "redirect to https://www.test.com2/aaa completed"
                }
                "/aab" {
                    HTTP::respond 302 Location "https://www.test.com/aab"
    				#log local0. "redirect to https://www.test.com2/aab completed"
                }
                "/aac" {
                    HTTP::respond 302 Location "https://www.test.com/aac"
    				#log local0. "redirect to https://www.test.com2/aac completed"
                }
                "/aad" {
                    HTTP::respond 302 Location "https://www.test.com/aad"
    				#log local0. "redirect to https://www.test.com2/aad completed"
                }
                "/aae" {
                    HTTP::respond 302 Location "https://www.test.com/aae"
    				#log local0. "redirect to https://www.test.com2/aae completed"
                }
                "/aaf" {
                    HTTP::respond 302 Location "https://www.test.com/aaf"
    				#log local0. "redirect to https://www.test.com2/aaf completed"
                }
    			"/aag" {
                    HTTP::respond 302 Location "https://www.test.com/aag"
    				#log local0. "redirect to https://www.test.com2/aag completed"
                }
            }
        }
    
    }