cancel
Showing results for 
Search instead for 
Did you mean: 
Login & Join the DevCentral Connects Group to watch the Recorded LiveStream (May 12) on Basic iControl Security - show notes included.

iRule to reroute to new site while evaluating variables

SteveEason
Nimbostratus
Nimbostratus

We have to route a website to a new website while maintaining the variables in the URL, and then append a new variable. I have that iRule working. However, there is a complication. There are 3 variables that will be changing on the new site. We need to evaluate those variables and change them during the redirect, while still appending the new variable. Here is the iRule that is working.

 

when HTTP_REQUEST {

 

  if { [string tolower [HTTP::host]] eq "www.oldwebsite.com"} { 

    HTTP::redirect "https://www.newwebsite.com[HTTP::uri]&fcRedirect=true"

  }

}

 

This irule correctly reroutes to the new website, maintains anything in the URL and the appends the new code to the end "&fcRedirect=true".

 

Now we have 3 tags that need to be rerouted. I tried the following iRule but it doesn't work.

 

 when HTTP_REQUEST {

  if { [string tolower [HTTP::host]] contains "Code1"}{

    HTTP::redirect "https://www.newwebsite.com/Code1&fcRedirect=true"}

  if { [string tolower [HTTP::host]] starts_with "Code2"}{

    HTTP::redirect "https://www.newwebsite.com/Code2&fcRedirect=true" }  

  if { [string tolower [HTTP::host]] starts_with "Code3"}{

    HTTP::redirect "https://www.newwebsite.com/Code3&fcRedirect=true"}

  else {

    HTTP::redirect "https://www.newwebsite.com[HTTP::uri]&fcRedirect=true"}

    }

 

Any suggestions on how to make this work for both situations? Maybe we need to make them separate irules that get processed individually? We're kinda on a time crunch and I'm just not finding any other options at the moment. Appreciate any feedback.

1 ACCEPTED SOLUTION

Hi SteveEason,

 

string tolower: Returns lowercase value.

when HTTP_REQUEST { if { [string tolower [HTTP::host]] contains "code1"} { HTTP::redirect "https://www.newwebsite.com/Code1&fcRedirect=true" } elseif { [string tolower [HTTP::host]] starts_with "code2"} { HTTP::redirect "https://www.newwebsite.com/Code2&fcRedirect=true" } elseif { [string tolower [HTTP::host]] starts_with "code3"} { HTTP::redirect "https://www.newwebsite.com/Code3&fcRedirect=true" } else { HTTP::redirect "https://www.newwebsite.com[HTTP::uri]&fcRedirect=true" } }

switch version:

when HTTP_REQUEST { switch -glob [string tolower [HTTP::host]] { "*code1*" { HTTP::redirect "https://www.newwebsite.com/Code1&fcRedirect=true" } "*code2*" { HTTP::redirect "https://www.newwebsite.com/Code2&fcRedirect=true" } "*code3*" { HTTP::redirect "https://www.newwebsite.com/Code3&fcRedirect=true" } default { HTTP::redirect "https://www.newwebsite.com[HTTP::uri]&fcRedirect=true" } } }

If codeX tags are contained in uri instead of host, you should use [HTTP::uri] instead of [HTTP::host].

when HTTP_REQUEST { switch -glob [string tolower [HTTP::uri]] { "*code1*" { HTTP::redirect "https://www.newwebsite.com/Code1&fcRedirect=true" } "*code2*" { HTTP::redirect "https://www.newwebsite.com/Code2&fcRedirect=true" } "*code3*" { HTTP::redirect "https://www.newwebsite.com/Code3&fcRedirect=true" } default { HTTP::redirect "https://www.newwebsite.com[HTTP::uri]&fcRedirect=true" } } }

 

View solution in original post

2 REPLIES 2

Hi SteveEason,

 

string tolower: Returns lowercase value.

when HTTP_REQUEST { if { [string tolower [HTTP::host]] contains "code1"} { HTTP::redirect "https://www.newwebsite.com/Code1&fcRedirect=true" } elseif { [string tolower [HTTP::host]] starts_with "code2"} { HTTP::redirect "https://www.newwebsite.com/Code2&fcRedirect=true" } elseif { [string tolower [HTTP::host]] starts_with "code3"} { HTTP::redirect "https://www.newwebsite.com/Code3&fcRedirect=true" } else { HTTP::redirect "https://www.newwebsite.com[HTTP::uri]&fcRedirect=true" } }

switch version:

when HTTP_REQUEST { switch -glob [string tolower [HTTP::host]] { "*code1*" { HTTP::redirect "https://www.newwebsite.com/Code1&fcRedirect=true" } "*code2*" { HTTP::redirect "https://www.newwebsite.com/Code2&fcRedirect=true" } "*code3*" { HTTP::redirect "https://www.newwebsite.com/Code3&fcRedirect=true" } default { HTTP::redirect "https://www.newwebsite.com[HTTP::uri]&fcRedirect=true" } } }

If codeX tags are contained in uri instead of host, you should use [HTTP::uri] instead of [HTTP::host].

when HTTP_REQUEST { switch -glob [string tolower [HTTP::uri]] { "*code1*" { HTTP::redirect "https://www.newwebsite.com/Code1&fcRedirect=true" } "*code2*" { HTTP::redirect "https://www.newwebsite.com/Code2&fcRedirect=true" } "*code3*" { HTTP::redirect "https://www.newwebsite.com/Code3&fcRedirect=true" } default { HTTP::redirect "https://www.newwebsite.com[HTTP::uri]&fcRedirect=true" } } }

 

SteveEason
Nimbostratus
Nimbostratus

Thank you for the clarification. So we were on the right track (at least I want to believe we were).

 

I've taken your last example and replicated that in our environment and it appears to be doing what we wanted. We will be implementing this in the production environment this afternoon and then we'll know for certain. I appreciate the input. I'll post one more time if everything goes well.

 

Thanks again!