Forum Discussion

SteveEason's avatar
Dec 11, 2020

iRule to reroute to new site while evaluating variables

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.

  • 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" }
    	}
    }
  • 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" }
    	}
    }
  • 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!