Problem with URL redirection

I wanted to redirect the webpages on this condition

if .xyz Domain Names | Join Generation XYZ redirect to www.abc.com

if http://xyz.com redirect to .xyz Domain Names | Join Generation XYZ

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

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]"
    }
}

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 :slight_smile: Shivam

Actually I just checked now the redirection for first link is not working. I mean when I go to .xyz Domain Names | Join Generation XYZ or .xyz Domain Names | Join Generation XYZ its not getting redirected.

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”.

I tried doing that..Did not work. Kind of no redirection is happening when I actually click on .xyz Domain Names | Join Generation XYZ which is actually .xyz Domain Names | Join Generation XYZ … Does it make a difference ?

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.

Just did the troubleshooting. so I added the test irule above the current one I am working and when I click on the link .xyz Domain Names | Join Generation XYZ 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

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 “.xyz Domain Names | Join Generation XYZ” } default { if { [TCP::local_port] != 443 } { HTTP::redirect “https://[HTTP::host][HTTP::uri]” } } } } ~~~

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.

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.

This is working

on my VS for https

when HTTP_REQUEST { if { [HTTP::uri] matches_regex {.ghjk.do} } { HTTP::redirect “” } if { [ string tolower [HTTP::uri]] equals"/" } { HTTP::redirect “” } if { [TCP::local_port] != 443 } { HTTP::redirect “https://[HTTP::host][HTTP::uri]” } }

On my VS for port 80

when HTTP_REQUEST { if {[TCP::local_port] != “443”} { HTTP::redirect “https://[HTTP::host][HTTP::uri]” } else { if { [HTTP::host] matches_regex {.ghjk.do}} { HTTP::redirect “” } elseif { [HTTP::uri] == “/”} { HTTP::redirect “” } } }

Thanks for your help Michael