Forum Discussion

cwdusheke's avatar
cwdusheke
Icon for Nimbostratus rankNimbostratus
Jan 23, 2020
Solved

irule - converting if to switch

I am trying to convert my irule from using the if to the switch. Here' s an email of my old irule.

 

when HTTP_REQUEST {

if { (([HTTP::host] contains "abc1.com") && ([HTTP::uri] == "/" )) }{

HTTP::uri "/tf/myPLAN/SponsorWelcome?cz=123456789"

}

elseif { (([HTTP::host] contains "abc2.com") && ([HTTP::uri] == "/" )) }{

HTTP::uri "/tf/myPLAN/SponsorWelcome?cz=223456789"

}

elseif { [string tolower [HTTP::host]] eq "example1.com" } {

    HTTP::respond 301 Location "https://example11.com"

  }

elseif { [string tolower [HTTP::host]] eq "example2.com" } {

    HTTP::respond 301 Location "https://example22.com"

  }

  • Hello.

    Try this:

    when HTTP_REQUEST {
    	switch -glob [string tolower [HTTP::host]] {
    		"*abc1.com" {
    			if { [HTTP::uri] eq "/" } {
    				HTTP::uri "/tf/myPLAN/SponsorWelcome?cz=123456789"
    			}
    		}
    		"*abc2.com" {
    			if { [HTTP::uri] eq "/" } {
    				HTTP::uri "/tf/myPLAN/SponsorWelcome?cz=223456789"
    			}
    		}
    		"*example1.com" {
    			HTTP::respond 301 Location "https://example11.com"
    		}
    		"*example2.com" {
    			HTTP::respond 301 Location "https://example22.com"
    		}
    	}
    }

    KR,

    Dario.

3 Replies

  • Hello.

    Try this:

    when HTTP_REQUEST {
    	switch -glob [string tolower [HTTP::host]] {
    		"*abc1.com" {
    			if { [HTTP::uri] eq "/" } {
    				HTTP::uri "/tf/myPLAN/SponsorWelcome?cz=123456789"
    			}
    		}
    		"*abc2.com" {
    			if { [HTTP::uri] eq "/" } {
    				HTTP::uri "/tf/myPLAN/SponsorWelcome?cz=223456789"
    			}
    		}
    		"*example1.com" {
    			HTTP::respond 301 Location "https://example11.com"
    		}
    		"*example2.com" {
    			HTTP::respond 301 Location "https://example22.com"
    		}
    	}
    }

    KR,

    Dario.

  • Thank you very much. I was looking through a lot of examples but not sure how to do it.

    • You're welcome :-)

      Please, don't forget to mark the answer as "the best" if it was helpful.