Forum Discussion

sundogbrew's avatar
sundogbrew
Icon for Altocumulus rankAltocumulus
Aug 28, 2019
Solved

Irule to redirect based on both IP and URI else default

Hey folks,

I am trying to write an Irule that redirects based on IP & URI. So I want to say if you are coming from this IP 1.1.1.1 and your URI contains /notcool then redirect to a URL, otherwise just send to the default pool. I have tried a few things and can get it to work for just the IP or just the URI but when I combine them I get no love.

Here is what I am working on.

This one below if I do the

if { $http_uri starts_with "/notcool"} on its own OR if I do

 

if { $client_ip equals "1.1.1.1"} without any mention of the other I am good,

but when I get greedy and try to do both that's when it falls apart.

 

when HTTP_REQUEST {

   set http_uri [HTTP::uri]

    set client_ip [IP::client_addr]

   if { $http_uri starts_with "/notcool"}{

     if { $client_ip equals "1.1.1.1"}{

       HTTP::redirect "https://coolnewpage.com/supercool"

   }

   else 

   {

     pool notcool_pool

   }

}

}

 

 

I also tried doing it like

 

when HTTP_REQUEST {

   set http_uri [HTTP::uri]

   set client_ip [IP::client_addr]

   if ( { $http_uri starts_with "/notcool"} && {$client_ip equals "1.1.1.1"}) {

       HTTP::redirect "https://coolnewpage.com/supercool"

   }

   else 

    {

     pool notcool_pool

   }

}

}

 

I appreciate any help you gurus can lend.

Thank you!

Joe

  • Sorry, try this. Couldn't quite remember where to put the parentheses.

     

    when HTTP_REQUEST {

    if { ([HTTP::uri] starts_with "/notcool") && ([IP::client_addr] equals "1.1.1.1")} {

    HTTP::redirect "https://coolnewpage.com/supercool"

    }

    else

    {

    pool notcool_pool

    }

    }

    }

4 Replies

  • Sorry, try this. Couldn't quite remember where to put the parentheses.

     

    when HTTP_REQUEST {

    if { ([HTTP::uri] starts_with "/notcool") && ([IP::client_addr] equals "1.1.1.1")} {

    HTTP::redirect "https://coolnewpage.com/supercool"

    }

    else

    {

    pool notcool_pool

    }

    }

    }

  • Try something like this? From what I've seen, when you've got two conditions to check, you have surround each with parentheses.

     

    when HTTP_REQUEST {

    if ( { [HTTP::uri] starts_with "/notcool"}) && ({[IP::client_addr] equals "1.1.1.1"}) {

    HTTP::redirect "https://coolnewpage.com/supercool"

    }

    else

    {

    pool notcool_pool

    }

    }

    }

  • Hey Atoth,

    I can't save it, i am getting this error not sure if I missed something in your response?

     

     Rule [/Common/test-jose] error: /Common/test-jose:2: error: [parse error: PARSE syntax 23 {syntax error in expression "(": premature end of expression}][(]

    /Common/test-jose:2: error: [undefined procedure: )][)]

    /Common/test-jose:2: error: [undefined procedure: &&][if ( { [HTTP::uri] starts_with "/notcool"}) && ({[IP::client_addr] equals "1.1.1.1"}) {

    HTTP::redirect "https://coolnewpage.com/supercool"

    }

    else

    {

    pool notcool_pool

    }]

    /Common/test-jose:10: error: [command is not valid in the current scope][}]

     

    Thanks

    Joe

  • Atoth,

    That did it! Thank you so much. I knew it was a parentheses issue but couldn't figure that one out! 😳

    Thank you!

    Joe