Forum Discussion

Robert_47833's avatar
Robert_47833
Icon for Altostratus rankAltostratus
May 12, 2011

if,elseif,else, need help

when HTTP_REQUEST {

 

set uri [HTTP::path]

 

set queryString [URI::query [HTTP::uri]]

 

set port [TCP::local_port]

 

set srcIP [IP::client_addr]

 

 

if {$uri equals "/"}

 

{ HTTP::redirect "http://m.srwd32.com/mobile/" }

 

elseif

 

{$uri starts_with "/myaccountapi/myaccount"}

 

{ persist none

 

pool SRWD32-MYX }

 

else{ if {$uri starts_with "/mobile/auth" and $port != 443 }

 

{ HTTP::redirect "https://[HTTP::host][HTTP::uri]"

 

log local0. "here i am 1 [HTTP::uri]" }

 

else

 

{ persist none

 

SSL::disable serverside

 

pool SRWD32-STATIC

 

log local0. "here i am 2 [HTTP::uri]" } } }

 

 

there is some issue in this irule

 

how to change it

 

 

if $uri is / go to http://m.srwd32.com/mobile/

 

if $uri starts_with "/myaccountapi/myaccount go to pool SRWD32-MYX

 

if {$uri starts_with "/mobile/auth" and $port != 443 go to https://[HTTP::host][HTTP::uri]

 

for other go to pool SRWD32-STATIC

 

 

 

I know I have mixed up by the realation of if ,elseif,else,..xo if......

 

 

 

Help me ?

7 Replies

  • Hi jucao,

    Try this and see if it works for what you are needing:

    
    when HTTP_REQUEST {
    switch -glob [ string tolower [HTTP::uri]] {
    "/" { HTTP::redirect "http://m.srwd32.com/mobile/" }
    "/myaccountapi/myaccount*" { persist none; pool SRWD32-MYX }
    }
    if { [[TCP::local_port clientside] != 443] && [[HTTP::uri] starts_with "/mobile/auth"] } {
    HTTP::redirect "https://[getfield [HTTP::host] ":" 1]/[HTTP::uri]"
    log local0. "here i am 1 [HTTP::uri]"
    }
    else {
    persist none
     SSL::disable serverside
    pool SRWD32-STATIC
     log local0. "here i am 2 [HTTP::uri]"
    }
    }
    
  • Hi Michael,

     

     

    If you click Edit and then Save on your post the ampersands will be rendered correctly.

     

     

    Aaron
  • Every time I have done that in the past it distorts the formatting of the entire post :-(

     

  • Thanks ,it seems switch works more effective than if.elseif,else,right?

     

     

    Any ,I found issue with this irule ,esle should be put behind },maybe it is sensitive to the } position.

     

    if {$uri equals "/"}

     

    { HTTP::redirect "http://m.srwd32.com/mobile/" }

     

    elseif

     

    {$uri starts_with "/myaccountapi/myaccount"}

     

    { persist none

     

    pool SRWD32-MYX }

     

    else{ if {$uri starts_with "/mobile/auth" and $port != 443 }

     

    { HTTP::redirect "https://[HTTP::host][HTTP::uri]"

     

    log local0. "here i am 1 [HTTP::uri]"

     

    }else

     

    { persist none

     

    SSL::disable serverside

     

    pool SRWD32-STATIC

     

    log local0. "here i am 2 [HTTP::uri]" } } }
  • I think the problem is with no spaces before or after the elses:

     

     

    else{ if {$uri starts_with "/mobile/auth" and $port != 443 }

     

    ...

     

    }else

     

     

    If you put spaces before and after the else statements it should load. But yes, the switch statement should be more efficient than the if/elseif/.../else chain.

     

     

    Aaron
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    Generally speaking we always encourage people to use switch over if/else chains where it makes sense. They're more efficient once you get past only a few if/else comparisons.

     

     

    Colin