Forum Discussion
URL rewrite through iRule
Hi Guys, i have one "Performance (HTTP)" virtual server on F5-1600 series, and i want to change the URL "http://www.abc.com" to "http://partner.abc.com/xyz". i have tried all below scripts :
1- when HTTP_REQUEST { if {([string tolower [HTTP::host]] equals "http://www.abc.com")}{ HTTP::header replace Host "http://partner.abc.com/xyz" } }
2- when HTTP_REQUEST { if { not ([HTTP::uri] starts_with "/xyz") } { HTTP::uri /xyz[HTTP::uri] } }
3- when HTTP_REQUEST { if {[HTTP::uri] equals {http://www.abc.com}} {HTTP::uri {http://partner.abc.com/xyz} } }
but i wasn't successful! can anyone help me how can i do this through iRule ?
Hi Sunnyman,
This will work;
if { [IP::addr [IP::client_addr] equals 1.1.1.1] || [IP::addr [IP::client_addr] equals 1.1.1.2} { SNAT with 1.1.1.100 snatpool VM_SNAT_POOL_1_1_1_100 } elseif { [IP::addr [IP::client_addr] equals 1.1.1.11] || [IP::addr [IP::client_addr] equals 1.1.1.12} { SNAT with 1.1.1.100 snatpool VM_SNAT_POOL_1_1_1_200 }
But that's not a very scalable way of doing it if your list is going to grow. If it's going to grow then use an address datagroup, with values snatpool names, which you can use like this
snatpool [class match -value [IP::remote_addr] equals dg_sunnyman_snatpools]
or more correctly;
set snatpool [class match -value [IP::remote_addr] equals dg_sunnyman_snatpools] if {$snatpool ne "") { snatpool $snatpool }
- sunnyman67_1367Nimbostratus
Thanks a lot joanna for your quick answer, ok , let me try it today or tomorrow and tell you the results...
- IheartF5_45022Nacreous
HTTP::uri "/gallery?[HTTP::query]"
Sorry to be brief - on phone.
- sunnyman67_1367Nimbostratus
Hi Joanna, i checked your last solution, it worked well for some VSs on my load-balancer, but i countered to one another small issue: do you know how can i change only the Context part of a URL? for example, when clinets want to see "www.abc.com/photos?tid5335gfgf53..." , in addition of URL-rewrite, i want to change the context part (fisrt part) of related URI (e.g. to "www.xyz.com/gallery?tid5335gfgf53..."). So, i don't want to change last part of URI (parameters). I want only to change first part of URI (in my example, "photos" to "gallery" with the same parameters as first URI). can you help me again joanna? thanks a lot for your regards...
- sunnyman67_1367Nimbostratus
Joanna, thanks a lot for your regards to my issue, i've checked your new solution yesterday, i think it must be worked properly, i'm testing it in different situations, i should finish my test process as soon as possible, and tell you the results. So let me to do my final check process with our network & vm team. I hope it'll be solved...
- sunnyman67_1367Nimbostratus
Joanna, thanks a lot for your regards to my issue, i've checked your new solution yesterday, i think it must be worked properly, i'm testing it in different situations, i should finish my test process as soon as possible, and tell you the results. So let me to do my final check process with our network & vm team. I hope it'll be solved...
- IheartF5_45022Nacreous
Sunnyman - Event LB_SELECTED definitely works on 10.2.4, however if you have problems just do it the way you originally planned ;-
HTTP::header replace Host "1.1.1.1:8080" HTTP::uri "/xyz"
but you must add in also a "node " or "pool " command also if you want to send traffic to a pool other than the default.
Joanna
- sunnyman67_1367Nimbostratus
Thanks Joanna, i've checked your new solution, but "LB_SELECTED" was not supported. My F5 load-balancer OS version is 10.2.4, but it doesn't support it. Also, i think that the URL i used in L7 is important, because it should be present on destination server, and in my scenario there is no domain name (URL) on the destination server. So, it's important to fill URL with true value. Now, how can i rewrite the URL "http:/abc.com/ukm/" on server 2.2.2.2 , to URL "http://1.1.1.1:8080/xyz/" on server 1.1.1.1 ?
- IheartF5_45022Nacreous
So do you really need to change the Host header at all? It has no effect on where the F5 sends the traffic BTW.
when HTTP_REQUEST { if {[string tolower [HTTP::host]] starts_with "abc.com" && [string tolower[HTTP::path]] eq "/ukm"} { This pool needs to be created with pool member 1.1.1.1:8080 pool pl_sunnyman_8080 HTTP::uri "/xyz" } } when LB_SELECTED { This will update the Host header with the IP:port of the selected server HTTP::header replace Host "[LB::server addr]:[LB::server port]" }
This way when you want to add more servers into your pool, the Host header will get rewritten with whatever server got chosen from the pool, but as I said, unless you have a particular reason to rewrite the Host header I wouldn't bother - it has no effect on routing.
- sunnyman67_1367Nimbostratus
Hi Joanna, Thanks again, certainly your solution is fine in related scenarios, but yet there is one small issue: as i mentioned before, i want to change the url to "1.1.1.1:8080/xyz", meanwhile there is no URL on 1.1.1.1. In fact, the new URL that i want to rewrite or to use is "1.1.1.1:8080/xyz". So, i don't want to use "partner.abc.com" in new Host header, i just want to send traffic to new node pool 1.1.1.1:8080 , but at the end of this address the "/xyz" URI should be inserted and makes "1.1.1.1:8080/xyz".Do you have any other idea about this issue?
- IheartF5_45022Nacreous
Hi Sunnyman,
No you are not quite right - while it's perfectly valid to include an IP/port number within a Host: header, this does not tell the F5 where to send the traffic. You need to use the node or pool command to inform TMOS where to send traffic. This is the most obvious way using node;-
if {[string tolower [HTTP::host]] starts_with "abc.com" && [string tolower[HTTP::path]] eq "/ukm"} { HTTP::header replace Host "partner.abc.com" node 1.1.1.1 8080 HTTP::uri "/xyz" }
Now having said that, it's not the way I would do it. You should include member 1.1.1.1:8080 in a pool, ie pl_sunnyman_8080 with an appropriate healthcheck and then instruct the F5 to send to the pool;-
if {[string tolower [HTTP::host]] starts_with "abc.com" && [string tolower[HTTP::path]] eq "/ukm"} { HTTP::header replace Host "partner.abc.com" pool pl_sunnyman_8080 HTTP::uri "/xyz" }
The reason this is better is that by having your server in a pool with an appropriate healthcheck, you can have multiple servers in the pool for backup, and also TMOS will already know the state of the servers in the pool, so will only choose one that is UP.
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com