Forum Discussion

arripay_6298's avatar
arripay_6298
Icon for Nimbostratus rankNimbostratus
Jun 28, 2016

Redirect Problem

Hi, using a single and pool with multiple hosts, trying to do a redirect based on host name and uri match using the following.

 

when HTTP_REQUEST { log local0. "Request: [HTTP::host][HTTP::uri]" if {[HTTP::host] equals "website1.com"}{ if { ([HTTP::uri] contains "app/commission") or ([HTTP::uri] contains "api/tickets") or ([HTTP::uri] contains "api/manageclasses") or ([HTTP::uri] contains "app/trainer") or ([HTTP::uri] contains "app/target") } { HTTP::redirect "www.website2.com" }} }

 

If I select website1.com/app/commission matches but redirects to website1.com/app/commission/www.website2.com

 

Any help greatly appreciated

 

8 Replies

  • Avoid multiple uri in same condition. I believe

    matchclass
    will be good option.

    Use below irule.

        class redirectURIs {
         /abc/book
         /manage/pqr
        /checkin/xyz
        }
    
            when HTTP_REQUEST {
             if { [matchclass [HTTP::uri] contains redirectURIs] } {
              HTTP::redirect "http://www.website2.com"
             }
            }
    
    • THi's avatar
      THi
      Icon for Nimbostratus rankNimbostratus
      "matchlass" is depreciated command already in sw v10. You should use "class" commands instead. See: https://clouddocs.f5.com/api/irules/class.html
    • THi's avatar
      THi
      Icon for Nimbostratus rankNimbostratus
      Also the request uri could be converted to lower case to allow mixed upper and lower case letters in it. Use tolower command.: [string tolower [HTTP::uri]]
    • arripay_6298's avatar
      arripay_6298
      Icon for Nimbostratus rankNimbostratus
      Thanks THi, really appreciate you help, proper stuck on this. I have had amend slightly as the devs have changed the redirect to a page on the same host. I cant get access to the f5 at the moment but I think this what I have, no change of behaviour. Will add tolower when I can get on machine. Any further help greatly appreciated. when HTTP_REQUEST { log local0. "Request: [HTTP::host][HTTP::uri]" if {([HTTP::uri] contains "app/commission") or ([HTTP::uri] contains "api/tickets") or ([HTTP::uri] contains "api/manageclasses") or ([HTTP::uri] contains "app/trainer") or ([HTTP::uri] contains "app/target") } { HTTP::redirect "[HTTP::host]//restricted" } }
  • I would recommend using datagroup with "class match" instead of "matchclass" as this has been deprecated with the 10.x code version and later.

     

    Try this:

     

    when HTTP_REQUEST { 
        log local0. "Request: [HTTP::host][HTTP::uri]" 
        if {[HTTP::host] equals "website1.com"} { 
            if { ([HTTP::uri] contains "app/commission") or ([HTTP::uri] contains "api/tickets") or ([HTTP::uri] contains "api/manageclasses") or ([HTTP::uri] contains "app/trainer") or ([HTTP::uri] contains "app/target") } { 
                 HTTP::respond 301 Location "http://www.website2.com/" 
                }
            } 
    }
  • Hi guys, seem to have it working, I think I needed a more exact match on the uri, thanks to THi for your assistance, most certainly helped!

       when HTTP_REQUEST {
    log local0. "Request: [HTTP::host][HTTP::uri]"
        if {([HTTP::uri] ends_with "/app/commission/commission-submission.html") or 
            ([HTTP::uri] ends_with "/app/classmanagement/class-management.html") or
            ([HTTP::uri] ends_with "/app/classticket/ticket-search-template.html") or
            ([HTTP::uri] ends_with "/app/trainer/trainer.html") or
            ([HTTP::uri] ends_with "/app/target/target.html") or
            ([HTTP::uri] ends_with "/app/target/target-setup-country.html") or
            ([HTTP::uri] ends_with "/app/studio/studio.html") }
            {
            HTTP::redirect "https://[HTTP::host]/app/shared/restricted.html"
                }
    }
    
    • THi's avatar
      THi
      Icon for Nimbostratus rankNimbostratus
      you're welcome, arripay