Forum Discussion

zhaleh_329474's avatar
zhaleh_329474
Icon for Nimbostratus rankNimbostratus
Jan 08, 2019

f5 irule redirect based on source ip

Hello

 

I want redirect to for specific 5 subnet ip address,and If these 5 addresses were not requested, they should be redirected to another address example to .

 

  • Hi,

     

    Here is a code sample that may help you :

     

    when HTTP_REQUEST {
        if { [IP::client_addr] eq "x.x.x.x" and [HTTP::host] eq "www.aaa.com" } {
            HTTP::redirect http://www.bbb.com[HTTP::uri]
        } else {
            HTTP::redirect http://www.ccc.com[HTTP::uri]
        }
    }

    That's just an example. You can have a look at IP::client_addr or class match commands to integrate a more complex lookup for a range of IP addresses.

     

    You can also replace HTTP::redirect command by a HTTP::respond command.

     

    Hope it helps

     

    Yann

     

    • Hamish's avatar
      Hamish
      Icon for Cirrocumulus rankCirrocumulus

      That's probably because it was (As Yann stated) an example...

       

      if you'd like some more help please at least tell us what you actually tried, what went wrong and what diagnostics/debugging you attempted to find the issue.

       

      if including a copy of your iRule, please put the code fences around it.

       

  • Hi,

     

    Here is a code sample that may help you :

     

    when HTTP_REQUEST {
        if { [IP::client_addr] eq "x.x.x.x" and [HTTP::host] eq "www.aaa.com" } {
            HTTP::redirect http://www.bbb.com[HTTP::uri]
        } else {
            HTTP::redirect http://www.ccc.com[HTTP::uri]
        }
    }

    That's just an example. You can have a look at IP::client_addr or class match commands to integrate a more complex lookup for a range of IP addresses.

     

    You can also replace HTTP::redirect command by a HTTP::respond command.

     

    Hope it helps

     

    Yann

     

    • Hamish's avatar
      Hamish
      Icon for Cirrocumulus rankCirrocumulus

      That's probably because it was (As Yann stated) an example...

       

      if you'd like some more help please at least tell us what you actually tried, what went wrong and what diagnostics/debugging you attempted to find the issue.

       

      if including a copy of your iRule, please put the code fences around it.

       

  • zhaleh,

    Try this iRule

    when HTTP_REQUEST {
        if {[IP::addr 10.0.0.0/8 equals [IP::client_addr]]} {
            HTTP::redirect http://bbb.com[HTTP::uri]
        } else {
            HTTP::redirect http://ccc.com[HTTP::uri]
        }
    }
    

    You can use the IP::addr command to compare the subnets of the IP address. The if statement would also work like this:

    if {[IP::addr "10.0.0.0 mask 255.0.0.0" equals [IP::client_addr]]} {