Forum Discussion

muzammil_88686's avatar
muzammil_88686
Icon for Nimbostratus rankNimbostratus
Dec 11, 2012

Optimizing Multiple URL redirections

Dear Dev Team,

 

 

I have a request to redirect the below URLs

 

 

www.xyz.com/p/cal2013/m1 to www.xyz.com/w/c/MC/SC/CAL2013/m1

 

www.xyz.com/p/cal2013/m2 to www.xyz.com/w/c/MC/SC/CAL2013/m2

 

www.xyz.com/p/cal2013/m3 to www.xyz.com/w/c/MC/SC/CAL2013/m3

 

www.xyz.com/p/cal2013/m4 to www.xyz.com/w/c/MC/SC/CAL2013/m4

 

www.xyz.com/p/cal2013/m5 to www.xyz.com/w/c/MC/SC/CAL2013/m5

 

www.xyz.com/p/cal2013/m6 to www.xyz.com/w/c/MC/SC/CAL2013/m6

 

www.xyz.com/p/cal2013/m7 to www.xyz.com/w/c/MC/SC/CAL2013/m7

 

www.xyz.com/p/cal2013/m8 to www.xyz.com/w/c/MC/SC/CAL2013/m8

 

www.xyz.com/p/cal2013/m9 to www.xyz.com/w/c/MC/SC/CAL2013/m9

 

www.xyz.com/p/cal2013/m10 to www.xyz.com/w/c/MC/SC/CAL2013/m10

 

www.xyz.com/p/cal2013/m11 to www.xyz.com/w/c/MC/SC/CAL2013/m11

 

www.xyz.com/p/cal2013/m12 to www.xyz.com/w/c/MC/SC/CAL2013/m12

 

 

We already have an exiting iRule for redirection to lot of other URLs and I do not want to add all the above 12 lines of the statements. Rather I would like to add fewer lines. Is it possible to do it with "findstr"?

 

Could you pls let me know the iRule which I can include in the existing iRule?

 

2 Replies

  • I'd suggest you make use of a Data Group. Here's an example;

    
    Create a Data Group with string and value pairs of the source URI and 
    associated URI to be redirected to. E.g string=/olduri1, value=/newuri1
    
    when HTTP_REQUEST {
     Compare the request URI with the strings in the uri-dg data group
     and populate the variable: redirect with the associated value if 
     there is a match
     set redirect [class match -value [HTTP::uri] equals uri-dg]
       
      As long as our variable: redirect isn’t empty, perform the redirect
      if { $redirect ne "" } {
       HTTP::redirect $redirect 
      } 
    
      If there wasn’t a match and variable: redirect is empty, do something
      else instead
      else {
       log local0. "No redirect occurred for: [HTTP::uri]"    
      }
    }
    
  • If all the incoming requested URLs contain consistent uri content, you might be able to use substr to dynamically pull the info you need from the end of the uri. Something like this (untested; just off the top of my head):

     

     

    when HTTP_REQUEST {

     

    if { [HTTP::uri] starts_with "/p/cal2013/" } {

     

    HTTP::redirect www.xyz.com/w/c/MC/SC/CAL2013/[substr [HTTP::uri] 11]

     

    }

     

    }