Forum Discussion

Domai_23823's avatar
Domai_23823
Icon for Nimbostratus rankNimbostratus
May 09, 2018

URI redirect iRule review/Help

Hello Guys, I have a question or review request for the below iRule. let me tell you about the requirement first. The user needs redirect to any URI that starts with /monkey/* BUT does not want 2 URL's which has /monkey/ to be redirected.

Like do not redirect

https://main.you.com/monkey/std/public/v1
and
https://main.you.com/monkey/public/v2

But redirect any other URL that has URI - /monkey/*

The iRule that I came up with and does not work is below. What am I doing wrong?

when HTTP_REQUEST {  
if { ([string tolower [HTTP::host]] eq "main.you.com") and ([string tolower [HTTP::uri]] eq "/monkey/std/public/v1") } {
                HTTP::redirect "https://main.you.com/monkey/std/public/v1"    
}
elseif { ([string tolower [HTTP::host]] eq "main.you.com") and ([string tolower [HTTP::uri]] eq "/monkey/public/v2") } {
                HTTP::redirect "https://main.you.com/monkey/public/v2"    
}
elseif { [string tolower [HTTP::host]] eq "main.you.com" } and ([string tolower [HTTP::uri]] eq "/monkey/*") } {
        HTTP::redirect "https://side.you.com"
else { use pool main.you.com_pool
    }
}

7 Replies

  • Did some modification in irule. Either use

    start_with or contains
    place of
    eq
    in syntex. It will work. Please let us know if any issue

        when HTTP_REQUEST {  
        if { ([string tolower [HTTP::host]] eq "main.you.com") and ([string tolower [HTTP::uri]] starts_with "/monkey/std/public/v1") } {
                HTTP::redirect "https://main.you.com/monkey/std/public/v1"    
            }
        elseif { ([string tolower [HTTP::host]] eq "main.you.com") and ([string tolower [HTTP::uri]] starts_with "/monkey/public/v2") } {
                HTTP::redirect "https://main.you.com/monkey/public/v2"    
            }
        elseif { ([string tolower [HTTP::host]] eq "main.you.com") and ([string tolower [HTTP::uri]] starts_with "/monkey/*") } {
        HTTP::redirect "https://side.you.com" }
        else { pool main.you.com_pool }
    } 
    
    • Domai's avatar
      Domai
      Icon for Altostratus rankAltostratus

      Thank you f5_rock I tried contains or start_with it did not work. It goes into a loop.

       

    • Nandhini_Natara's avatar
      Nandhini_Natara
      Icon for Nimbostratus rankNimbostratus

      Hello Domai,

      Please try with below irule

      when HTTP_REQUEST {

      if { ([string tolower [HTTP::host]] starts_with "main.you.com") and ([string tolower [HTTP::uri]] starts_with "/monkey/std/public/v1") } {
              HTTP::redirect "[https://main.you.com/monkey/std/public/v1"](https://main.you.com/monkey/std/public/v1);  
      
          }
      elseif { ([string tolower [HTTP::host]] starts_with "main.you.com") and ([string tolower [HTTP::uri]] starts_with "/monkey/public/v2") } {
              HTTP::redirect "[https://main.you.com/monkey/public/v2"](https://main.you.com/monkey/public/v2);  
      
          }
      elseif { ([string tolower [HTTP::host]] starts_with "main.you.com") and ([string tolower [HTTP::uri]] starts_with "/monkey/") } {
      HTTP::redirect "[https://side.you.com"](https://side.you.com); }
      else { pool main.you.com_pool }
      

      }

    • Domai's avatar
      Domai
      Icon for Altostratus rankAltostratus

      Nandhini I tried that with both "starts_with" and "contains" it did not work as intended. Jie's iRule did work.

       

  • JG's avatar
    JG
    Icon for Cumulonimbus rankCumulonimbus

    I hope this is what you need:

     when HTTP_REQUEST {
        if { [string tolower [HTTP::host]] equals "main.you.com" } {
            switch -glob [string tolower [HTTP::path]] {
                "/monkey/std/public/v1*" -
                "/monkey/std/public/v2*" {
                    pool main.you.com_pool
                }
                "/monkey/*" {
                    HTTP::redirect "https://side.you.com/"
                }
                default {
                    pool main.you.com_pool
                }
            }
        }
    }