Forum Discussion

Michael_193050's avatar
Michael_193050
Icon for Nimbostratus rankNimbostratus
Jul 16, 2015

redirect based on host and uri

Hi i have the below irule which is working fine. I'm trying to make it more efficient and only issue one redirect if the request came in as https://oldsite.com/. Currently what happens is i get the first redirect, then the second one when the uri matches /.

The existing irule:

    when HTTP_REQUEST {  
    if {[string tolower [HTTP::host]] eq "oldsite.com"} {
        HTTP::redirect "https://newsite.com[HTTP::uri]"
    }
        elseif {[string tolower [HTTP::uri]] eq "/"} {
            HTTP::redirect "https://newsite.com/identitymanagement/default.aspx"    
        }
}

The new iRule I'm attempting to get working is:

    when HTTP_REQUEST {  
    if { ([string tolower [HTTP::host]] eq "oldsite.com") and ([string tolower [HTTP::uri]] eq "/") } {
        HTTP::redirect "https://newsite.com/identitymanagement/default.aspx"
    }
        elseif {[string tolower [HTTP::uri]] eq "/"} {
            HTTP::redirect "https://newsite.com/identitymanagement/default.aspx"    
        }
}

This fixes the multiple redirects.

The problem i'm having is if i get a request for https://oldsite.com/path1, i still need to redirect to the https://newsite.com/path1. I can't figure out how to also take into account this. I've also tried converting this to a switch statement, but wasn't able to find a way to check both http::host and http::uri to formulate the correct redirect.

Thanks,

3 Replies

  • I added this and it's working fine. Let me know if there is a more efficient way of doing this thru a switch statement.

    when HTTP_REQUEST {  
        if { ([string tolower [HTTP::host]] eq "oldsite.com") and ([string tolower [HTTP::uri]] eq "/") } {
            HTTP::redirect "https://newsite.com/identitymanagement/default.aspx"
        }
            elseif {[string tolower [HTTP::uri]] eq "/"} {
                    HTTP::redirect "https://newsite.com/identitymanagement/default.aspx"    
                    }
            elseif {[string tolower [HTTP::host]] eq "oldsite.com"} {
                    HTTP::redirect "https://newsite.com[HTTP::uri]" 
            }
    }
    

    Thanks

  • I believe you can actually get what you need from the first iRule, but simply invert the cases, as in:

    when HTTP_REQUEST {  
        if { [HTTP::uri] eq "/" } {
            HTTP::redirect "https://newsite.com/identitymanagement/default.aspx"    
        } elseif { [string tolower [HTTP::host]] eq "oldsite.com" } {
            HTTP::redirect "https://newsite.com[HTTP::uri]"
        }
    }
    

    That way, if the request is "http[s]://oldsite.com/" or "http[s]://newsite.com/", the redirect will be to "https://newsite.com/identitymanagement/default.aspx". Otherwise, a redirect occurs if and only if the Host header is "oldsite.com". (Notice that I removed the

    string tolower
    on the
    HTTP::uri
    verification, since it isn't needed for this comparison case).

    Instead of using an iRule, however, you should consider employing Local Traffic Policies if you are using 11.4 or above, or HTTP Classes if you are using a version lower than that:

    Generally, these mechanisms are more efficient than an iRule.

  • Thanks for the response, switching the cases worked exactly like i needed. I'm familiar with the policies, i just really haven't explored how to accomplish the same things i'm used to doing in irules.