Forum Discussion

Allanwynn_16283's avatar
Allanwynn_16283
Icon for Nimbostratus rankNimbostratus
Oct 17, 2015

Redirect to an internal site based on client source network (IP)

Hi all,

 

I am not really familiar with iRule, so can you help me:

 

  1. An iRule where it is based on client's source network (not specific IP) to be redirected to an internal site?

i.e. 172.16.4.0 network should be redirected to http://mysite1.com (internal site) and any other network will be redirected to http://mysite2.com (internal site)

 

  1. An iRule based on client's source ip but will be based if it is odd or even number (is this possible)?

i.e. 172.16.4.1, 172.16.6.7, 172.16.10.23, etc. should be redirected to http://mysite1.com (internal site) and any other even IP's will be redirected to http://mysite2.com (internal site)

 

Or if number 2 is not possible, do you have any recommendations?

 

Thank you!

 

4 Replies

  • Hi

    Try these?

    iRule 1

    when HTTP_REQUEST {
        if { [IP::addr [IP::client_addr]/24 equals 192.168.1.0/24] } {
            HTTP::respond 301 Location "http://mysite1.com"
        } else {
            HTTP::respond 301 Location "http://mysite2.com"
        }
    }
    

    iRule 2

    when HTTP_REQUEST {
    
        set lastoctet [lindex [split [IP::client_addr] .] 3]
    
        if { [expr $lastoctet % 2] == 1 } {
            HTTP::respond 301 Location "http://mysite1.com" 
        } else {
            HTTP::respond 301 Location "http://mysite2.com"
        }
    }
    

    /Patrik

  • It's to check if the IP is even or odd. If it's 172.16.4.15 it will be 15 % 2, which is 1. If it's 172.16.4.14 it will be 14 % 2, which is 0.

    when HTTP_REQUEST {
    
        Get the last octet. For 172.16.4.11 that would be 11
        set lastoctet [lindex [split [IP::client_addr] .] 3]
    
        Check if the octet is odd or even with modulus (remainder)
        if { [expr $lastoctet % 2] == 1 } {
            HTTP::respond 301 Location "http://mysite1.com" 
        } else {
            HTTP::respond 301 Location "http://mysite2.com"
        }
    }
    

    /Patrik

  • Looks like you have two guys helping there. Might be more confusing to add me to the mix. :)