Forum Discussion

bwilliam's avatar
bwilliam
Icon for Cirrus rankCirrus
Dec 03, 2015

irule To Redirect A Specific Network Going To a URL

I have an office in India and need to send any request from that network for site.domain.com to local.domain.com. If the network is not equal India_Host_Networks then hit site.domain.com. I think I have part of the irule, but need help finishing it.

 

when HTTP_REQUEST { if { [class match [IP::client_addr] equals India_Host_Networks]) and ( [HTTP::host] equals "site.domain.com") } { HTTP::redirect " https://local.domain.com/stream/" } }

 

9 Replies

  • Hi,

    there was a missing parenthesis probably causing the failure to load the iRule:
    when HTTP_REQUEST {
        if { ([class match [IP::client_addr] equals India_Host_Networks]) and ([string tolower [HTTP::host]] equals "site.domain.com") } {
            HTTP::redirect " https://local.domain.com/stream/"
        }
    }
    

    To be on the safe side I added a lower case conversion for the hostname. Just make sure to match versus lower case strings as well, please.

    Thanks, Stephan
  • Thanks Stephan! I actually didn't apply the irule yet, but thanks for the correction. Is there something we can add in case a network hits the irule that isn't part of the india group? This way the traffic would continue on to site.domain.com and not redirect to local.domain.com/stream.

     

  • Thank you both for your answers. I have an existing irule we call Universal Redirect can the irule we worked on be applied in the Universal Redirect? Below is the start of the irule. If I add the new code like the below will that work?

     

    when HTTP_REQUEST {
       switch -glob [string tolower [HTTP::host]] {
          "*sitename.domain.com*" {
              add the redirect link to "sitename.domain.com"
             HTTP::redirect "https://sitename.domain.com"
          }
          "*sitename.domain.com*" {
             HTTP::redirect "https://sitename.domain.com"
          }
       if { ([class match [IP::client_addr] equals India_Host_Networks]) and ([string tolower [HTTP::host]] equal "site.domain.com") } {
            HTTP::redirect " https://local.domain.com/stream/"
          }
          default {
              add the default action you prefer ie "www.def.com"
             HTTP::redirect "https://[HTTP::host][HTTP::uri]"
          }
       }
    }
  • Hi,

    this would i.e. look like the following:
    when HTTP_REQUEST {
       switch [string tolower [HTTP::host]] {
          "sitename1.domain.com" {
              add the redirect link to "sitename.domain.com"
             HTTP::redirect "https://sitename1.domain.com"
             return
          }
          "sitename2.domain.com" {
             if { [class match [IP::client_addr] equals India_Host_Networks] } {
                 HTTP::redirect " https://local.domain.com/stream/"
                 return
             } else {
                 HTTP::redirect "https://sitename2.domain.com"
                 return
             }
          }
          default {
              add the default action you prefer ie "www.def.com"
             HTTP::redirect "https://[HTTP::host][HTTP::uri]"
             return
          }
       }
    }
    

    From my perspective it is best practice to add a return after using HTTP::redirect or HTTP::response because it can be used only once in the context of HTTP_REQUEST.

    Thanks, Stephan
  • Thanks for the tip Stephan, so in the case of below where I have several redirects it should look like the below.

    Also, I found out there are some requirements that I missed. Basically, we are doing a video redirect from one server to the next. That said I will need to create 2 irules for these redirects and maintain the variable from the client in the redirect. Using the irule we worked on, how do we maintain the client variable.

    Redirect 1 https://media.domain.com:8443/stream/{Variable} redirect to https://InhydNimble1.domain.com/stream/{Variable} Redirect 2 https://media.domain.com:8443/video/{Variable} redirect to https://InhydNimble1.domain.com/video/{Variable}

          }
       if { ([class match [IP::client_addr] equals India_Host_Networks]) and ([string tolower [HTTP::host]] equal media.domain.com") } {
            HTTP::redirect " https://InhydNimble1.domain.com/"
            return
          }
          "*change*" {
             HTTP::redirect "http://site.domain.com/cm"
             return
          }
          "*empcon*" {
             HTTP::redirect "http://site.domain.com/?cmd=login"
             return
          }
          "*im*" {
             HTTP::redirect "https://site.domain.com/IntegratedLogin.asp"
             return
          }
          "*helpdesk*" {
             HTTP::redirect "https://site.domain.com/IntegratedLogin.asp"
             return
          }
          "*ipplan" {
             HTTP::redirect "https://site.domain.com/ipplan "
             return
          }
          "*schedule" {
             HTTP::redirect "http://site.domain.com/arrivemrp/ "
             return
          }
    
  • It would look like the following to stick to your example with the string "media" contained in the hostname:

      }
      "*media*" {
           if { [class match [IP::client_addr] equals India_Host_Networks] } {
               HTTP::redirect " https://InhydNimble1.domain.com[HTTP::uri]"
               return
           } else {
               HTTP::redirect " https://media.domain.com[HTTP::uri]"
               return
           }
      }
      "*change*" {
         HTTP::redirect "http://site.domain.com/cm"
         return
      }
      "*empcon*" {
         HTTP::redirect "http://site.domain.com/?cmd=login"
         return
      }
      "*im*" {
         HTTP::redirect "https://site.domain.com/IntegratedLogin.asp"
         return
      }
    

    By adding the

    [HTTP::uri]
    to the redirect you will preserve the initial path including variables. There is no need for an additional hostname evaluation. So I removed it. (It will be evaluated by the switch command and match the "*media*" as you are using the globbing option.)

  • Thanks Stephan! So to summarize, the above rule will direct request for media.domain.com to InhydNimble1.domain.com if they are part of the India_Host_Networks.

     

  • Perfect! I will test it out. Again thanks so much for your help Stephan.