cancel
Showing results for 
Search instead for 
Did you mean: 
Login & Join the DevCentral Connects Group to watch the Recorded LiveStream (May 12) on Basic iControl Security - show notes included.

http redirect with multiple condetion

Mohammad_1363
Altocumulus
Altocumulus

Hello

I am trying to redirect http request coming to my F5s to another path, i have a working iRule but the new requirement is, since my VIP has multiple A records, the developers do not know which one is correct therefore i need to change my iRule logic.

I am putting the final destination as a pool in my F5 due to some challenges which we had and by adding the destination in my F5 as a pool we do not have any issue.

can you please check if below iRule is good or do you see any issue, all tree http request for aaa.com bbb.com and ccc.com should go to one endpoint which is api.com

 

Thanks

Mohammad

 

 

when HTTP_REQUEST {

 if { [string tolower [HTTP::host]] equals "aaaa.com" } {

      HTTP::header replace "Host" "api.com"

      set current_uri [HTTP::uri]

      HTTP::uri "/something$current_uri"

      pool new-pool

  }

 

elseif { [string tolower [HTTP::host]] equals "bbbb.com" } {

      HTTP::header replace "Host" "api.com"

      set current_uri [HTTP::uri]

      HTTP::uri "/something$current_uri"

      pool new-pool

 }

 

elseif { [string tolower [HTTP::host]] equals "cccc.com" } {

      HTTP::header replace "Host" "api.com"

      set current_uri [HTTP::uri]

      HTTP::uri "/something$current_uri"

      pool new-pool

 }

 

}

 

 

8 REPLIES 8

SanjayP
MVP
MVP

I'm assuming you want to send traffic for those 3 hosts to a pool with serverside rewrite and not a clientside redirect (Where URL would be changed and visible to client). So using pool is correct option.

 

But if traffic for all 3 hosts going to same pool and also there is not other custom requirement for specific host, you can club them together using OR condition. But switch statement would be better.

 

for last default one you can either use default_pool, reject (reject if host header is coming something diff than defined) or return (to stop processing iRule further and use default assigned pool to vip for other hosts)

 

when HTTP_REQUEST { switch -glob [string tolower [HTTP::host]] { "aaaa.com" - "bbbb.com" - "cccc.com" { HTTP::header replace "Host" "api.com" set current_uri [HTTP::uri] HTTP::uri "/something$current_uri" pool new-pool } default { return } } }

 

Hello Sanjay

Thanks for your recommendation, is the below one correct an issue with this iRule ?

when HTTP_REQUEST {

 if { [string tolower [HTTP::host]] equals "aaaa.com"] ||

      [string tolower [HTTP::host]] equals "bbbb.com"] ||

      [string tolower [HTTP::host]] equals "cccc.com"] } {

       HTTP::header replace "Host" "api.com"

       set current_uri [HTTP::uri]

       HTTP::uri "/something$current_uri"

       pool new pool

       #log local0. "URI: [HTTP::uri] Host: [HTTP::host]"

 }

 }

}

Please use switch command instead, the one I posted above.

Sure, To increase or my knowledge, Is this iRule also correct or not ?

when HTTP_REQUEST {

 if { [string tolower [HTTP::host]] equals "aaaa.com"] ||

      [string tolower [HTTP::host]] equals "bbbb.com"] ||

      [string tolower [HTTP::host]] equals "cccc.com"] } {

       HTTP::header replace "Host" "api.com"

       set current_uri [HTTP::uri]

       HTTP::uri "/something$current_uri"

       pool new pool

       #log local0. "URI: [HTTP::uri] Host: [HTTP::host]"

 }

 }

}

 

seems okay. I would just use return command after pool or else to finish the processing of iRule for other host traffic.

like this ?

when HTTP_REQUEST {

 if { [string tolower [HTTP::host]] equals "aaaa.com"] ||

      [string tolower [HTTP::host]] equals "bbbb.com"] ||

      [string tolower [HTTP::host]] equals "cccc.com"] } {

       HTTP::header replace "Host" "api.com"

       set current_uri [HTTP::uri]

       HTTP::uri "/something$current_uri"

       pool new pool

       #log local0. "URI: [HTTP::uri] Host: [HTTP::host]"

}

default {

return

}

}

 

 

when HTTP_REQUEST {   if { [string tolower [HTTP::host]] equals "aaaa.com"] ||   [string tolower [HTTP::host]] equals "bbbb.com"] ||   [string tolower [HTTP::host]] equals "cccc.com"] } {   HTTP::header replace "Host" "api.com"   set current_uri [HTTP::uri]   HTTP::uri "/something$current_uri" pool new pool   #log local0. "URI: [HTTP::uri] Host: [HTTP::host]" return } } }  

 

Thanks a lot Sanjay