Forum Discussion

Tony2020's avatar
Tony2020
Icon for Nimbostratus rankNimbostratus
Apr 28, 2017

Match & redirect based on URIs to another site with similar URIs structured

Hi all,

 

I would like to know if anyone can help provide recommendations on this iRUle where it will only match based on the URIs from the old site, and from there redirect to a new site with similar URIs. The new site will have the exact same URIs, so it would be simple as it is 1 to 1 matching.

 

The example is below is using a data group, and I have not tested this code yet, but if anyone can suggest a better way to do this, all suggestion appreciated.. The goal here is to make it where it's easy to add/remove and modify, and using the data group string is the best way to do so as the there are only one new site to go to.

 

Note, i am running 11.5.x code

 

class my_uri_datagroup {
   {
      "/uri1/ my.newsite.com"
      "/uri2/ my.newsite.com"
      "/uri3/ my.newsite.com"
      "/uri4/ my.newsite.com"
      "/uri5/ my.newsite.com"
      "/uri6/ my.newsite.com"
      "/uri7/ my.newsite.com"
      "/uri8/ my.newsite.com"
      "/uri9/ my.newsite.com"
   }
}

 when HTTP_REQUEST {
  set newhost [findclass [string tolower [URI::path [HTTP::uri] 1 1]] my_uri_datagroup " "]
  if { [string tolower [HTTP::host]] equals "my.oldsite.com" and $newhost ne "" } {
    HTTP::redirect "http://$newhost[HTTP::uri]"
  }
 }
}

my.oldsite.com/uri1 -> my.newsite.com/uri1

 

my.oldsite.com/uri2 -> my.newsite.com/uri2

 

my.oldsite.com/uri3 -> my.newsite.com/uri3

 

my.oldsite.com/uri4 -> my.newsite.com/uri4

 

my.oldsite.com/uri5 -> my.newsite.com/uri5

 

my.oldsite.com/uri6 -> my.newsite.com/uri6

 

my.oldsite.com/uri7 -> my.newsite.com/uri7

 

my.oldsite.com/uri8 -> my.newsite.com/uri8

 

my.oldsite.com/uri9 -> my.newsite.com/uri9

 

4 Replies

  • If all the condition will be same like above, Then i will write simple iRule like below.

        when HTTP_REQUEST {
            set uri [string tolower [HTTP::uri]]
            if { [string tolower [HTTP::host]] equals "my.oldsite.com" } {
            HTTP::redirect "http://my.newsite.com$uri"
                 }
        }
    
    • Tony2020's avatar
      Tony2020
      Icon for Nimbostratus rankNimbostratus

      thank you and appreciate your suggestion on this. I will try this.

       

    • Tony2020's avatar
      Tony2020
      Icon for Nimbostratus rankNimbostratus

      forgot to mentioned. In your code, where is the section to pull in the data group that contains the list of URIs? We want to only match based on those URIs, so if there is a match, it will redirect. If there are no match with URIs, than it should not get redirected and should stay on "my.oldsite.com".

       

      Thank you.

       

  • Hi,

    looking at your question, I think irule / Datagroup is not the best solution but LTM Policies.

    You can create one policy with rules:

    • rule 1:
      • condition : HTTP uri path starts with
        /uri1/ /uri2/ /uri3/ /uri4/ /uri5/ /uri5/
      • action http-reply
        tcl:"http://my.newsite.com[HTTP::uri]"
    • rule 2:
      • condition : HTTP uri path starts with
        /uri6/ /uri7/ /uri8/ /uri9/
      • action http-reply
        tcl:"http://my.newsite.com2[HTTP::uri]"

    the tcl format is supported since version 12.0. In previous version, the

    tcl:
    string must be removed.