Forum Discussion

gdoyle's avatar
gdoyle
Icon for Cirrostratus rankCirrostratus
Oct 03, 2018

Multiple URL's redirecting the URI's.

I have a customer that is based out of multiple states, each states website is "mywebsiteXX.com" where XX is the state code (e.g. NY for New York, FL for Florida).

 

All of these come in to the same VIP (VIP is called mywebsitexx.com), but are then pointed to the their states website. If they have the URI "getstarted" or "feedback" the customer wants them redirected to "/web/xx/getstarted" or "web/xx/feedback" where xx is the state code.

 

Would something like this work in the irule or is this not going to detect the URL properly?

 

switch -glob [string tolower [HTTP::uri]] {
       "/getstarted*"  {
           switch -glob [string tolower [HTTP::host]] {
               "*mywebsiteaz*"  { HTTP::respond 301 "Location" "https://mywebsiteaz.com/web/xx/getstarted" }
               "*mywebsitefl*"  { HTTP::respond 301 "Location" "https://mywebsitefl.com/web/xx/getstarted" }
               "*mywebsiteny*"  { HTTP::respond 301 "Location" "https://mywebsiteny.com/web/xx/getstarted" }
               default      { discard }
           }
       }

       "/feedback*"  {
           switch -glob [string tolower [HTTP::host]] {
               "*mywebsiteaz*"  { HTTP::respond 301 "Location" "https://mywebsiteaz.com/web/xx/feedback" }
               "*mywebsitefl*"  { HTTP::respond 301 "Location" "https://mywebsitefl.com/web/xx/feedback" }
               "*mywebsiteny*"  { HTTP::respond 301 "Location" "https://mywebsiteny.com/web/xx/feedback" }
               default      { discard }
           }
       }
}

1 Reply

  • Your iRule looks fine and should work, but you're repeating two very similar blocks of code. You could achieve the same functionality by using something like this with less lines

     

    when HTTP_REQUEST {
    
         use getfield to capture the first field of the URI
        set uriField [string tolower [getfield [HTTP::uri] / 2]]
        if {($uriField eq "getstarted") || ($uriField eq "feedback")} {
            switch -glob [string tolower [HTTP::host]] {
                   "*mywebsiteaz*" { HTTP::respond 301 "Location" "https://mywebsiteaz.com/web/xx/$uriField" }
                   "*mywebsitefl*" { HTTP::respond 301 "Location" "https://mywebsitefl.com/web/xx/$uriField" }
                   "*mywebsiteny*" { HTTP::respond 301 "Location" "https://mywebsiteny.com/web/xx/$uriField" }
                   default { discard }
            }
        }
    }

    You could even do away with the switch statement if you used a datagroup for host to state lookups:

     

    Datagroup:

     

    ltm data-group internal states_dg {
        records {
            mywebsiteaz {
                data az
            }
            mywebsiteaz {
                data fl
            }
            mywebsiteny {
                data ny
            }
        }
        type string
    }

    irule:

     

    when HTTP_REQUEST {
        set uriField [string tolower [getfield [HTTP::uri] / 2]]
        if {($uriField eq "getstarted") || ($uriField eq "feedback")} {
            set state [class match -value [HTTP::host] contains state_dg]
            if {$state ne ""} {
                HTTP::respond 301 "Location" "https://[HTTP::host]/web/$state/$uriField"
            }
        }
    }