Forum Discussion

pedinopa_170325's avatar
pedinopa_170325
Icon for Nimbostratus rankNimbostratus
Sep 14, 2016

irule question

I want to write an iRule that will look at the client IP and direct the to a specific URL. I need some help with the irule

 

if CLIENT_IP -LIKE X.X.X.X [ HTTP_REQUEST HTTP://SERVER/INDEX.HTM ]

 

once this is working I will need to be able to add multiple IP ranges.

 

5 Replies

  • I would recommend that you write a simple example without worrying about syntax and semantics by searching online. Drop the finished version and people can help you out to fine tune it. This way you will end up learning too. Just a friendly suggestion :-)

     

  • I have been doing some searching and reviewing log files I am trying to redirect based on client IP or should I be using remote_ip

     

    this is what I have so far to try to redirect 1 IP then I need to do arange of IPs (192.168.1.0/24)

     

    when HTTP_REQUEST { if { ( [IP::addr [IP::client_addr] equals x.x.x.x]) } { HTTP::redirect "http:///index_new.htm" } }

     

  • HTTP::redirect will give you a 302 redirect. For 301 redirect, you would have to utilize:

    HTTP::respond 301 Location "http://[HTTP::host]/index_new.htm"

    Fixing some syntax issues:

    when HTTP_REQUEST { 
    if { [IP::addr [IP::client_addr] equals x.x.x.x] } { 
    HTTP::redirect "http://[HTTP::host]/index_new.htm"
      }
    }
    
  • You can also use the IP::addr command to check if an IP address is in a particular network range. For example:

    if { [IP::addr [IP::client_addr] equals 10.10.10.0/24] } {
        HTTP::redirect....
    }
    

    IP::client_addr is the same as IP::remote_addr on client-side events, such as HTTP_REQUEST.

  • Hi pedinopa@gmail.com,

    the best approach would be to use a LTM datagroup to store the different IP Addresses / Subnets and to use the

    [class]
    command to lookup those IPs within an iRule and then trigger the redirect. If you need to add additional IPs then you don't need to change your iRule. Just add a new entry into the data-group using the WebInterface.

    Data Group:

    ltm data-group internal DG_REDIRECT_IPs {
        records {
            1.1.1.1/32 { }
            2.2.2.2/32 { }
            3.3.3.3/32 { }
            10.0.0.0/8 { }
            172.16.0.0/12 { }
            192.168.0.0/16 { }
        }
        type ip
    }
    

    iRule:

    when HTTP_REQUEST {
        if { [class match [getfield [IP::client_addr] "%" 1] equals DG_REDIRECT_IPs]] } then {
            HTTP::redirect "http://SERVER/index.htm"
        }   
    }
    

    For further information you may take a look to the

    [class]
    command wiki page.

    https://devcentral.f5.com/wiki/iRules.class.ashx

    Cheers, Kai