Forum Discussion

Preet_pk's avatar
Preet_pk
Icon for Cirrus rankCirrus
Feb 05, 2022
Solved

Host redirection not working for second host in iRule

Hi,

There is one issue i am facing in redirection - For eg below is the scenerio.

test.com  -> https://test.com/redlab/login.html

lab.com -> https://lab.com/bluelab/login.html

We are using single Virtual Server & HTTP::host based irule for backend Pool connectivity.

Tried below irule for redirection - but redirection is working only for first host domain & is not working for second host domain ( for lab.com )

when HTTP_REQUEST {
if {[HTTP::host] equals "test.com" and [HTTP::uri] equals "/"} {
HTTP::redirect "https://test.com/redlab/login.htmll"
if {[HTTP::host] equals "lab.com" and [HTTP::uri] equals "/"} {
HTTP::redirect "https://lab.com/bluelab/login.html"}
}
}

Please let me know, if any modification to be done on abve irule for second host redirection also to work.

2 Replies

  • Hi Preet_pk

    this iRule will satisfy your requirements:

    when HTTP_REQUEST {
        if { [string tolower [HTTP::host][HTTP::path]] equals "domain-alpha.com.com/" } {
            HTTP::respond 301 -version 1.1 noserver Location "https://domain-alpha.com.com/redlab/login.html"
        } elseif { [string tolower [HTTP::host][HTTP::path]] equals "domain-beta.com.com/" } {
            HTTP::respond 301 -version 1.1 noserver Location "https://domain-beta.com.com/bluelab/login.html"
        } else {
          #default action
        }
    }
    • The 301 response code indicates that the requested page has been permanently moved to the URL given in the Location header.
    • -version 1.1 will set HTTP/1.1 (otherwise HTTP/1.0 would be used).
    • noserver will prevent that the default server header (configured in HTTP profile, usually BigIP) is returned to the client.

    I use HTTP::respond instead of HTTP::redirect, because:
    HTTP::respond can use 301 / Moved Permanently
    HTTP::redirect will alsways use 302 / Found or Temporary Moved

    Hope this helps and happy F5 day
    Daniel

  • You may try Daniel_Wolf  solution but your iRule has clear mistakes as the second "if" statement is in the first "if" statemet.

     

    when HTTP_REQUEST {

    if {[HTTP::host] equals "test.com" and [HTTP::uri] equals "/"} {
    HTTP::redirect "https://test.com/redlab/login.htmll" }
    if {[HTTP::host] equals "lab.com" and [HTTP::uri] equals "/"} {
    HTTP::redirect "https://lab.com/bluelab/login.html"}
    }

     

    or even better

     

     

     

    when HTTP_REQUEST {

    if {[HTTP::host] equals "test.com" and [HTTP::uri] equals "/"} {
    HTTP::redirect "https://test.com/redlab/login.htmll"
    }elseif {[HTTP::host] equals "lab.com" and [HTTP::uri] equals "/"} {
    HTTP::redirect "https://lab.com/bluelab/login.html"}
    }

     

    Alse this free article for writting irules (the "then" statement is just optional):

    https://community.f5.com/t5/technical-articles/irules-101-02-if-and-expressions/ta-p/283431