Forum Discussion
Want to rewrite part of host
Hi guys, I'm still new to iRules, I wonder that if it is possible that an iRule can rewrite part of the host instead of all of it.
I mean like, if a client request a host like "123.aaa.com/888/", he will be redirected to the "123.bbb.com/888/", I just want to rewrite the "aaa.com" part, which is when requests to host like "xxx.aaa.com/xxx/" I'd like it to be redirected to "xxx.bbb.com/xxx/", is this possible?
Thanks guys.
If string position is known then you can use
function to grep value like below.getfield
set hostname [getfield [HTTP::host] "." 1] If the URL is xxx.aaa.bbb.com, then getfield command will grep value of 1st position xxx set hostname1 [getfield [HTTP::host] "." 2] If the URL is xxx.aaa.bbb.com, then getfield command will grep value of 2nd position aaa
Refer below link. Hope it will be helpful.
- Andy_McGrathCumulonimbus
Do you want to do a HTTP redirect or just change the Host Header before you forward to a pool member?
yes, you can pretty much do everything on that. all, a part, multiple parts, nothing :)
check the documentation on a string manipulation: https://devcentral.f5.com/articles/the101-irules-101-string-handling-and-manipulation
https://devcentral.f5.com/articles/irules-101-13-tcl-string-commands-part-1
it is just a matter of writing the code to do what you want.
- KimiLi_147173Nimbostratus
Want to redirect to another website.
I've already got that how to write an irule to rewrite the whole host a client requests to get, but how to rewrite part of the host, and make clients go to it, that's my issue, cause xxx.aaa.com, by the string xxx I mean it's a dynamic one.
- MR_FreddyNimbostratus
can you test this?
when HTTP_REQUEST { if {[string tolower [HTTP::host]] eq "xxx.aaa.com" }{ HTTP::redirect "http://xxx.bbb.com[HTTP::uri]" } }
- KimiLi_147173Nimbostratus
That's not my purpose, by xxx I mean a dynamic string, it could be xxx, 123, aaa or whatever. Thanks anyway.
- Samir_Jha_52506Noctilucent
If string position is known then you can use
function to grep value like below.getfield
set hostname [getfield [HTTP::host] "." 1] If the URL is xxx.aaa.bbb.com, then getfield command will grep value of 1st position xxx set hostname1 [getfield [HTTP::host] "." 2] If the URL is xxx.aaa.bbb.com, then getfield command will grep value of 2nd position aaa
Refer below link. Hope it will be helpful.
- KimiLi_147173Nimbostratus
Hi f5_rock,
YOU'RE THE MAN!
I believe this is what I want, according to your suggestion I've got an irule like this:
when HTTP_REQUEST { Check if host contains "aaa.com" if {[HTTP::host] contains "aaa.com"}{ Get the xxx from host xxx.aaa.com HTTP::redirect "https://[getfield [HTTP::host] "." 1].bbb.com[HTTP::uri]" } }
And it works! I'll keep on doing further tests to figure out if there's still room to make it better.
Thanks again!
- Samir_Jha_52506Noctilucent
Thank you so much KimiLi!!
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