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, i've checked your solution and it's OK and works fine! Thanks for your regards and quick replies joanna...
- sunnyman67_1367Nimbostratus
Thanks a lot joanna, so let me to check all situations and tell you the results...
- IheartF5_45022Nacreous
It will work in either CLIENT_ACCEPTED or HTTP_REQUEST, however CLIENT_ACCEPTED would be most efficient as you only make the decision once per-TCP connection.
I can't give you a definite answer on the other question, but one approach would be to use Automap in the configuration and then to use the snatpool statement. Or just leave it blank in the config and rely on the snatpool in the iRule.
- sunnyman67_1367Nimbostratus
Thanks joanna for your useful help (like ever), but in which event should i use this "if" block? (HTTP_REQUEST? CLIENT_ACCEPTED? , ...). In addition of this question, i want to know what should i set the "SNAT" filed of VS configuration page (Auto-map , None , ...)? Because, i'm going to use of iRule for SNAT operation, in this situation what should i set the corresponding configuration field in related part of VS configuration?
- IheartF5_45022Nacreous
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
Hi joanna, i have one another question: i have one VS, i want to do SNAT through different SNAT Pool List upon the client source IP address. For example, if the source ip address of client is 1.1.1.1/24 or 1.1.1.2/24, VS should changes the source ip address of outgoing packet to 1.1.1.100, and if the source ip address of client is 1.1.1.11/24 or 1.1.1.12/24, VS should changes the source ip address of outgoing packet to 1.1.1.200. I've checked it with below script, but it didn't work! Am i wrong? Can you help me? Is there any need to use of HTTP_REQUEST or some other methods???
when HTTP_REQUEST {
switch -glob [IP::client_addr] {
"1.1.1.1" - "1.1.1.2" { SNAT with 1.1.1.100 snatpool VM_SNAT_POOL_1_1_1_100 } "1.1.1.11" - "1.1.1.12" { SNAT with 1.1.1.200 snatpool VM_SNAT_POOL_1_1_1_200 }
} }
- sunnyman67_1367Nimbostratus
Hi again joanna, i've checked both commands. For some of URLs they are ok, but for some of them not! Do you have any other idea to maintain query part of a URI and only change the path part of that URI, except these three blow commands ???
HTTP::uri "/ABC/newPics?[URI::query [HTTP::uri]]" HTTP::uri /ABC/newPics"?[HTTP::query]" HTTP::uri "/ABC/newPics?[HTTP::query]"
- sunnyman67_1367Nimbostratus
OK joanna, so let me try it and tell you the final results as soon as possible, Thanks a lot for your regards joanna...
- IheartF5_45022Nacreous
Yes you are right!! Was trying to do from memory - should be;
HTTP::uri "/gallery?[URI::query [HTTP::uri]]
- sunnyman67_1367Nimbostratus
Excuse me joanna, are both of below commands same?
1) HTTP::uri "/gallery?[HTTP::query]" 2) HTTP::uri "/gallery?[URI::query [HTTP::uri]]"
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