Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology
Clear all filters
Answers

f5 irule redirect based on source ip

Hello

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

1
Rate this Question

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

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

0
Comments on this Answer
Comment made 2 months ago by zhaleh 125

i test it but don't work

0
Comment made 2 months ago by Hamish 3391

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.

0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

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]]} {
0
Comments on this Answer
Comment made 2 months ago by zhaleh 125

how can use data group for ip because i have 65 range and take it in irule

0
Comment made 2 months ago by Rico 778

This should work for your issue:

when HTTP_REQUEST {
    if {[class match [IP::client_addr] equals subnet_list]} {
        HTTP::redirect http://bbb.com[HTTP::uri]
    } else {
        HTTP::redirect http://ccc.com[HTTP::uri]
    }
}

When you create the datagroup, ensure the type is 'type: address' so that it will calculate the addresses properly.

0
Comment made 2 months ago by zhaleh 125

Where can i see thw type?there is no specified type in here...when i open the drop box infront of the type field,adress,integer,external file and string apears...in none of them i can find it

0
Comment made 2 months ago by Rico 778

Sorry, 'Type: address' is the correct setting. I mistyped the name.

0
Comment made 2 months ago by zhaleh 125

ok never mind (: when i add address 5.0.0.0/8 in datagroup it gives me the wrong result ... i mean it shows the both site bbb.com and ccc.com one after another ,i think it does not calculate subnet mask .

0
Comment made 2 months ago by Rico 778

That's strange. It works as expected in my lab environment. Try running a tcpdump from the command line interface to make sure the source address is what you expect it to be. If you share the results of the tcpdump, maybe I can help you figure out what the issue is.

0
Comment made 2 months ago by zhaleh 125

Sorry i can not send tcp dump but i checked with tcpdump it work correctly, so I checked another device f5 but again wrong result:(

0