Forum Discussion

cgwin12's avatar
cgwin12
Icon for Altostratus rankAltostratus
Oct 07, 2024

I-rule redirect advise

I have tested an http redirect i-rule and this works. However, the user wants me to redirect to a completely different site for another uri request. 

 

For the first, ccsweb redirects to cta.lanl.gov as expected. 

 

when HTTP_REQUEST {
if ![HTTP::has_responded] {
#set request [HTTP::uri]

if {[string tolower [HTTP::host]] equals "ccsweb.lanl.gov" }{
# HTTP::respond 301 Location "https://cta.lanl.gov" Strict-Transport-Security "max-age=31536000"
HTTP::redirect https://cta.lanl.gov
return
}
}}

 

However, for they also want https://cssweb.lanl.gov/~pakin to redirect to https://lanlexperts.elsevierpure.com/en/persons/scott-d-pakin when inputting https://ccsweb.lanl.gov/~pakin.

 

I'm not sure if a redirect irule can work to 2 different sites from the same virtual server. The second irule looks like this. 

 

when HTTP_REQUEST {
if ![HTTP::has_responded] {
#set request [HTTP::uri]

if {[string tolower [HTTP::host]] equals "https://ccsweb.lanl.gov/~pakin" }{
# HTTP::respond 301 Location "https://cta.lanl.gov" Strict-Transport-Security "max-age=31536000"
HTTP::redirect https://lanlexperts.elsevierpure.com/en/persons/scott-d-pakin

return
}
}}

 

Any suggestions on combining these 2 i-rules? What is missing if the second redirect doesnt trigger? 

  • Hi cgwin12,

    [HTTP::uri]
    Returns or sets the URI part of the HTTP request.
    https://clouddocs.f5.com/api/irules/HTTP__uri.html

    [HTTP::path]
    Returns or sets the path part of the HTTP request. The path is defined as the path and filename in a request. It does not include the query string.
    https://clouddocs.f5.com/api/irules/HTTP__path.html

    [HTTP::query]
    Returns the query part of the HTTP request. The query is defined as the part of the request past a ? character, if any.
    https://clouddocs.f5.com/api/irules/HTTP__query.html

    [HTTP::host]
    Returns the value contained in the Host header of an HTTP request.
    https://clouddocs.f5.com/api/irules/HTTP__host.html

    For the following URL:
    https://mysite.com/content1/anythingelse.html?a=123

    [HTTP::uri]		: /content1/anythingelse.html?a=123
    [HTTP::path]	: /content1/anythingelse.html
    [HTTP::query]	: a=123
    [HTTP::host]	: mysite.com

    iRule:

    when HTTP_REQUEST {
        if { [HTTP::has_responded] } { return }
        if { [HTTP::host] eq "ccsweb.lanl.gov" && [string tolower [HTTP::uri]] eq "/~pakin" } {
            HTTP::redirect "https://lanlexperts.elsevierpure.com/en/persons/scott-d-pakin"
            return
        }
        elseif { [HTTP::host] eq "ccsweb.lanl.gov" } {
            HTTP::redirect "https://cta.lanl.gov"
            return
        }
    }

     

  • I tried combining like this below and but still get a 404 error on the 2nd part of the i-rule.  I go straight to that url and it works.

    when HTTP_REQUEST {
    if ![HTTP::has_responded] {
    #set request [HTTP::uri]

    if {[string tolower [HTTP::host]] equals "ccsweb.lanl.gov" and ([string tolower [HTTP::uri]] eq "/") }  {
    # HTTP::respond 301 Location "https://cta.lanl.gov" Strict-Transport-Security "max-age=31536000"
    HTTP::redirect https://cta.lanl.gov

        } elseif {[string tolower [HTTP::host]] equals "https://ccsweb.lanl.gov/~pakin" and ([string tolower [HTTP::uri]] eq "/~pakin") } {
        # HTTP::respond 301 Location "https://cta.lanl.gov" Strict-Transport-Security "max-age=31536000"
        HTTP::redirect https://lanlexperts.elsevierpure.com/en/persons/scott-d-pakin


        return
    }
    }}