Forum Discussion
Problem with URL redirection
I wanted to redirect the webpages on this condition
if http://xyz.com/j/k/l redirect to www.abc.com
if http://xyz.com redirect to https://xyz.com/a/b
When I configured the irule, first part is working but not the second one. So basically when we try accessing http://xyz.com its creating a redirect loop.
My irule
when HTTP_REQUEST { if { [HTTP::uri] == "/j/k/l" } { HTTP::redirect "https://www.abc.com/" }
elseif { [HTTP::uri] == "/" } {
HTTP::redirect "https://xyz.com/a/b"
}
else {
HTTP::redirect "https://[HTTP::host][HTTP::uri]"
}
}
Thanks Shivam
11 Replies
- Michael_Yates
Nimbostratus
Hi Shivam,
You have several options:
Using a Switch Statement (useful to start if you are going to be adding a lot of URI specific behavior):
when HTTP_REQUEST { switch -glob [ string tolower [HTTP::uri]] { "/j/k/l" { HTTP::redirect "http://www.abc.com" } "/" { HTTP::redirect "https://xyz.com/a/b" } default { if { [TCP::local_port] != 443 } { HTTP::redirect "https://[HTTP::host][HTTP::uri]" } } } }Or using a bunch of IF Statements (which is faster than a small SWITCH Statement (until you get above 4 or 5 IF Statements, then a SWITCH Statement is more efficient)).
when HTTP_REQUEST { if { [ string tolower [HTTP::uri]] equals "/j/k/l" } { HTTP::redirect "http://www.abc.com" } if { [ string tolower [HTTP::uri]] equals"/" } { HTTP::redirect "https://xyz.com/a/b" } if { [TCP::local_port] != 443 } { HTTP::redirect "https://[HTTP::host][HTTP::uri]" } } - Shivam_84461
Nimbostratus
Hello Michael, That worked like a charm..Thanks Just a quick question, what was the issue with the old syntax and why it was not working. Also one more thing that we have two VS one for http and one for https(ssl offloading). Your irule seems to be working for both of them. I guess its because of the last If statement correct ? Thanks again :) Shivam - Shivam_84461
Nimbostratus
Actually I just checked now the redirection for first link is not working. I mean when I go to http://xyz.com/j/k/l or https://xyz.com/j/k/l its not getting redirected. - Michael_Yates
Nimbostratus
The contents of a the comparisons are literal (so if there is anything else in the URI it will not match). You can correct this in the switch statement by changing "/j/k/l" to "/j/k/l*" (adding an *) and in the if statement by changing "equals" to "starts_with". - Shivam_84461
Nimbostratus
I tried doing that..Did not work. Kind of no redirection is happening when I actually click on http://xyz.com/j/k/l which is actually http://xyz.com/dfg/fgh/wer.do ... Does it make a difference ? - Michael_Yates
Nimbostratus
Yes. That will make a difference. If you want to troubleshoot then create another simple iRule (and make sure that it is applied to the Virtual Server above the iRule you are working on). ~~~ when HTTP_REQUEST { log local0. "Incoming URI: [ string tolower [HTTP::uri]]" } ~~~ Then run a tail -f on your /var/log/ltm log and try your test again. This will log the URI so you can see what the iRule is seeing. If it doesn't match anything in the iRule your working on, you will see why. - Shivam_84461
Nimbostratus
Just did the troubleshooting. so I added the test irule above the current one I am working and when I click on the link http://xyz.com/dfg/fgh/wer.do I do see the URI /dfg/fgh/wer.do, but still redirection is not working. May 21 15:03:49 F5-LTM2- info tmm[9636]: Rule /Common/irule-test : Incoming URI: /dfg/fgh/wer.do - Michael_Yates
Nimbostratus
Yep. That's why it's not working. You don't have that URI Listed in the iRule. If you want that to redirect then replace/add "/dfg/fgh/wer.do" into the switch statement. ~~~ when HTTP_REQUEST { switch -glob [ string tolower [HTTP::uri]] { "/j/k/l*" { HTTP::redirect "http://www.abc.com" } "/dfg/fgh/wer.do*" { HTTP::redirect "http://www.abc.com" } "/" { HTTP::redirect "https://xyz.com/a/b" } default { if { [TCP::local_port] != 443 } { HTTP::redirect "https://[HTTP::host][HTTP::uri]" } } } } ~~~ - Shivam_84461
Nimbostratus
This URI "/dfg/fgh/wer.do" was always there. The only thing not there was * , which I added but still it did not work. When I initially referred I just made it short, but then wanted to bring this up so that syntax might change in irule. Anyways still not working :(..I am scratching my head now.. Its hitting the irule, I just checked the irule statistics but just not redirecting. - Michael_Yates
Nimbostratus
Considering the information that you have given, what I provide should be working. At this point unless I see the actual iRule as well as know exactly what you are doing then there isn't much guidance that I can provide due to lack of information.
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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