Forum Discussion

DM_5174's avatar
DM_5174
Icon for Nimbostratus rankNimbostratus
May 19, 2011

Multiple URI matching redirect

Hi All,

I was trying to accoplish the following things using a class or "string tolower" one-on-one matching to redirect the URI.

Can anyone please help or could suggest the best way to accomplish this?

Site: http://www.mysite.com/old-service/old-app1

Objective:

1. rewrite the URI matching parameter defined in the class or use string tolower list if possible.

2. How do I match it where /old-service/old-app1 gets replaced with /new-service/new-app100 and so on...

*** (it needs to match exactly from the old URI to new URI) ***

Example: http://www.mysite.com/old-service/old-app1 ==> http://www.mysite.com/new-service/new-app100

Example: http://www.mysite.com/old-service/old-app2 ==> http://www.mysite.com/new-service/new-app200

Example: http://www.mysite.com/old-service/old-app3 ==> http://www.mysite.com/new-service/new-app300

Example: http://www.mysite.com/old-service/old-app4 ==> http://www.mysite.com/new-service/new-app400

Example: http://www.mysite.com/old-service/old-app5 ==> http://www.mysite.com/new-service/new-app500

Is it possible to create a class with the following parameter and pull it in from there:

"new-service/new-app100"

"new-service/new-app200"

"new-service/new-app300"

"new-service/new-app400"

"new-service/new-app500"


when HTTP_REQUEST {
  set app_path [string tolower [getfield [HTTP::host] "." 0] ]
  if { [matchclass $::newURI_apps contains $app_path] } {
    HTTP::redirect "http://www.mysite.com/$app_path[HTTP::uri]"
    }
}

Thanks all,

-DM

9 Replies

  • Hi DM,

     

     

    That looks like a good start. Which LTM version are you testing this for? If you're on 10.x you should replace matchclass with the class command and remove the $:: prefix from the datagroup name references in the iRule. You could then use class match -value to get the old service to new service mapping from the datagroup. The datagroup would be a string type with the key being the old app paths and the values being the new app path.

     

     

    For 9.x, you can use findclass with the datagroup set as a string with the old_app and new_app paths separated by a space.

     

     

    Aaron
  • Hi Aaron,

     

     

    We are using the 9x version. Can you give an example for the 9x with the findclass? What if we don't use the data class and just want to use the "string tolower" to achieve the matching exact URI and redirect to the new app?

     

     

    Thanks,

     

     

    -DM
  • Hi Aaron,

     

     

    I have tried the below code and could not get it to work. Is there anything wrong I am doing?

     

     

     

     

    
    
    when HTTP_REQUEST {
     if { [HTTP::host] equals "www.mysite.com" } {
       log local0. "User went to http://[HTTP::host][HTTP::uri]" 
       set site [HTTP::uri]
        switch -glob [string tolower [HTTP::uri]] { 
        
    "/old-service/old-app1*" { 
            HTTP::redirect "https://[HTTP::host]/new-service/new-app100" 
            }
          
            "/old-service/old-app2*" { 
            HTTP::redirect "https://[HTTP::host]/new-service/new-app200" 
            }
        
       "/old-service/old-app3*" { 
            HTTP::redirect "https://[HTTP::host]/new-service/new-app300" 
            }
        "/old-service/old-app4*" { 
            HTTP::redirect "https://[HTTP::host]/new-service/new-app400" 
            }
    
    "/old-service/old-app5*" { 
            HTTP::redirect "https://[HTTP::host]/new-service/new-app500" 
            }
           
         } 
    }
    
    

     

     

     

    Thanks,

     

    -DM
  • Hi DM,

     

    What kind of error or issue do you get? The only thing I am seeing missing is a missing "}" at the end of your code.

     

     

    Bhattman
  • Hi Bhattman,

     

     

    It was redirecting to a service not active on our web server. That is resolved now that the virtual directory is started.

     

     

    Is there an easier way in doing this? maybe call and match old URI to new URI in a data class group? If we get up to a hundred /app directories, the code can get quite messy and long. Please advise on this since I am sure you have seen this before.

     

     

    Thanks again,

     

    -DM
  • Yes you should be able to do this using datagroups. Here is a wiki shortcut that uses findclass command to use datagroups

     

     

    http://devcentral.f5.com/wiki/default.aspx/iRules/findclass.html

     

     

     

    I hope this helps

     

    Bhattman
  • HI Bhattman,

     

     

    I have followed the wiki and based on the example, how do I call the class in the irule? I tried to create a data group but could not figure out how to do so.

     

    How would you use the "class" statement with LTM version 9.x? Do I use the class statement in the actual irule or call it using a matchclass?

     

     

    thanks,

     

    DM

     

     

    class URIredirects {

     

    "/old-service/old-app1/ https://www.mysite.com/new-service/new-app100"

     

    "/old-service/old-app2/ https://www.mysite.com/new-service/new-app200"

     

    "/old-service/old-app3/ https://www.mysite.com/new-service/new-app300"

     

    "/old-service/old-app4/ https://www.mysite.com/new-service/new-app400"

     

    "/old-service/old-app5/ https://www.mysite.com/new-service/new-app500"

     

    }

     

     

    when HTTP_REQUEST {

     

    set location [findclass [HTTP::uri] $::URIredirects " "]

     

    if { $location ne "" }

     

    HTTP::redirect $location

     

    }

     

    }

     

  • Hi DM,

     

    You are actually calling up the class object in your iRule. I do want to clarify that "class URIRedirects" is not something you put into an iRule. You have to create a data group called URIredirects with type string and then enter "/old-service/old-app1/ https://www.mysite.com/new-service/new-app100".

     

     

    Bhattman
  • More replies here:

     

     

    http://devcentral.f5.com/Community/GroupDetails/tabid/1082223/asg/50/aft/1178985/showtab/groupforums/Default.aspx

     

     

    Aaron