Forum Discussion

Alan_Evans_1020's avatar
Alan_Evans_1020
Icon for Nimbostratus rankNimbostratus
May 24, 2011

iRule to redirect to new name after given time

Our customer is going through a name change and we will need to redirect from oldname to newname starting on a given date. I realize I could just apply the iRule at the right time BUT why not automate it?

 

 

There are at least a dozen sites that will need redirects and I imagine after the cutover date there will be a bunch of "oops we missed xyz.example.com" requests so rather than writing an iRule for each case and applying it only to the necessary VS I would like to write one iRule that handles all the VS.

 

 

There is an example that uses a data group and space separated old/new values but I can't imagine that the regex processing in that setup makes it particularly efficient. I figured an array would be a clever way to do this instead. Just create an array with hosts for keys and new URLs for values. Check the array for the existence of the current host and redirect if necessary. This also affords me the ability to handle http/https redirects. Many of the VS have a general rule that redirects HTTPtoHTTPS already so if I insert the name change rule ahead of those I won't end up with multiple redirects.

 

 

We're running 10.2.something I forget exactly what point/hotfix on 1600s.

 

 

Here's what I have that validates when I submit the iRule to the web UI but does not work when I try to hit it. I am having trouble w/ TCL syntax, exprs, etc etc etc.

 

 

when RULE_INIT {

 

array set static::redirects {

 

app.example.com https://app.newexample.com/

 

www.example.com http://www.example.com/

 

}

 

 

Epoch time for 6/1/2011 00:00:00

 

set static::cutovertime 1306900800

 

}

 

when HTTP_REQUEST {

 

if { ([clock seconds] > $static::cutovertime) && ([info exists $static::redirects([string tolower [HTTP::host]])]) } {

 

log local0. "Redirecting to: [redirects([HTTP::host])][HTTP::uri]"

 

HTTP::redirect "[redirects([HTTP::host])][HTTP::uri]"

 

}

 

}

 

 

Can anyone help w/ my syntax? I write Bash/POSIX/Perl daily but I just can't seem to get my head around TCL yet.

 

 

Regards,

 

-Alan

 

4 Replies

  • Hi Alan,

    The square braces act as backticks. Can you try this?

    when RULE_INIT {
      array set static::redirects {
        app.example.com https://app.newexample.com/
        www.example.com http://www.example.com/
      }
    
       Epoch time for 6/1/2011 00:00:00
      set static::cutovertime 1306900800
    }
    when HTTP_REQUEST {
      if { ([clock seconds] > $static::cutovertime) && ([info exists $static::redirects([string tolower [HTTP::host]])]) } {
        log local0. "Redirecting to: $static::redirects([string tolower [HTTP::host]])"
        HTTP::redirect $static::redirects([string tolower [HTTP::host]])
      }
    }
    

    Aaron
  • Thank you Aaron for steering me in the right direction. I did discover one more thing. The 'info exists $static::' needs to be 'info exists static::' as is the whole problem here I don't understand TCL syntax to know why...

    Here is the final result:

    when RULE_INIT {
      array set static::renameredirects {
        app.example.com https://app.newexample.com
        www.example.com http://www.newexample.com
      }
       Epoch time for 6/1/2011 00:00:00 EST
      set static::renamecutovertime 1306900800
    }
    when HTTP_REQUEST {
      if { ([clock seconds] > $static::renamecutovertime) && ([info exists static::renameredirects([string tolower [HTTP::host]])]) } {
        log local0. "Redirecting to: $static::renameredirects([string tolower [HTTP::host]])[HTTP::uri]"
        HTTP::redirect $static::renameredirects([string tolower [HTTP::host]])[HTTP::uri]
      }
    }

    Now we just put oldname newurl pairs in the RULE_INIT and no one has to wake up in the middle of the night to change it over on 6/1. Of course I will probably be awake then anyway... üôÇ

    Regards,

    -Alan

  • Sorry for missing that. The info command options expect a variable name without the $. I think the idea is that you're referencing the variable name, not the value of the variable.

     

     

    I assume you already plan this, but once the date passes, you could remove the date check logic and just do the redirect to save some resources.

     

     

    Aaron
  • Ha. Yeah probably. The one condition and extra global ought not be a significant amount of overhead but I am meticulous like that. I am very diligent to solve with iRules only what cannot be solved in the app itself. In this case the web/app teams are so overwhelmed with other changes that they pushed the redirects off on to LB...

     

     

    Thanks again,

     

    -Alan