Forum Discussion

C_D_18583's avatar
C_D_18583
Icon for Nimbostratus rankNimbostratus
Jun 27, 2006

Apache mapping and F5 Irules

Here is one Apache mapping using mod_rewrite.

 

/cgi-ads/jsp/viewitem.do?category=personal_services_local&Region=A http://at-consume.tsl.da.com:2200/telus-cp-portal-web/jsp/productcatalogue/personal/index.jsp?

 

 

I would like BIP IP to handle this redirect by using IRules something like:

 

 

RULE NAME : S_REDIRECT

 

VERSION : 1.0

 

DESC : Redirect

 

when HTTP_REQUEST {

 

if { [HTTP::uri] equals " /cgi-ads/jsp/viewitem.do?category=personal_services_local&Region=A" } {

 

HTTP::redirect "http://at-consume.tsl.da.com:2200/telus-cp-portal-web/jsp/productcatalogue/personal/index.jsp"

 

}

 

}

 

 

Is there a more elagant way to write this Irule? For example using datagroups?

8 Replies

  • If you are just doing a single rewrite, then data groups are overkill. This is probably as simple as it gets. If you need to have many redirects, then a data group might make a more readible iRule.

     

     

    -Joe
  • Joe

     

     

    I have many redirects. How do I contruct these statement using datagroups?
  • If all the URIS in a datagroup go to the SAME redirect, then you can use datagroups, but if all the redirects themselves are different I don't see a way to do it, you'll have to call them out individually.

    That is, if your datagroup called "Region1" includes the following URI's:

    /cgi-ads/jsp/viewitem.do?category=personal_services_local&Region=A

    /cgi-ads/jsp/viewitem.do?category=personal_services_local&Region=B

    /cgi-ads/jsp/viewitem.do?category=personal_services_local&Region=C

    /cgi-ads/jsp/viewitem.do?category=personal_services_local&Region=D

    and you want them ALL to be redirected to:

    http://at-consume.tsl.da.com:2200/telus-cp-portal-web/jsp/productcatalogue/personal/index.jsp

    then this should work:

    
    when HTTP_REQUEST {
    if { matchclass [HTTP::uri] equals $::Region1 } {
    HTTP::redirect "http://at-consume.tsl.da.com:2200/telus-cp-portal-web/jsp/productcatalogue/personal/index.jsp"
      }
    }

    But if the A,B,C,D variants above need separate redirects, then datagroups won't help you.

    Denny
  • Ahh, you learn something new every day.

    You can use store mappings in a datagroup. You can store both strings space delimited in the datagroup and use the findclass method to extract them.

    check out the findclass docs for some examples:

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

    Click here

    Let's say you have these four mappings defined in a string datagroup (this is the bigip.conf rendering of the data group)

    class redirects {
      /cgi-ads/jsp/viewitem.do?category=personal_services_local&Region=A http://redirect1.com/path1
      /cgi-ads/jsp/viewitem.do?category=personal_services_local&Region=B http://redirect2.com/path2
      /cgi-ads/jsp/viewitem.do?category=personal_services_local&Region=C http://redirect3.com/path3
      /cgi-ads/jsp/viewitem.do?category=personal_services_local&Region=D http://redirect4.com/path4
    }

    You could then use the following to lookup the first key and extract the second.

    when HTTP_REQUEST {
      set my_redirect [findclass [HTTP::uri] $::redirects " "]
      if { $my_redirect ne "" } {
        HTTP::redirect $my_redirect
      }
    }

    Keep in mind that the findclass command is not case sensitive so if you need to worry about both upper and lower cases in requests, lowercase the strings in the datagroup and use the string tolower command on the HTTP::uri variable.

    -Joe
  • You can use the datagroups for multiple URI and redirects:

    class Region1 {

    "URI1 mapped_redirect_URL1"

    "URI2 mapped_redirect_URL2"

    "URI3 mapped_redirect_URL3"

    "URI4 mapped_redirect_URL4"

    }

    
    when HTTP_REQUEST {
      if { matchclass [string tolower [HTTP::uri]] equals $::Region1 } {
        HTTP::redirect [findclass [string tolower [HTTP::uri]] $::Region1 " "]
      }
    }

    Note that the class is defined outside of the iRule window on the datagroups tab.

    HTH,

  • Looks like Joe beat me to it, and with an explanation to boot!
  • Thank you all the solutions. I tried the examples you provided and I must say it is a very clean way to handle this mapping instead of the IF statements.

     

     

    One more question:

     

     

    My re-directs depend on cookie values. So if Region=A AND Type=Res then I need to use map to re-direct, else follow original URI. And if no cookies are present direct user to a set region apps so the appropriate cookies can be set.

     

     

    Let me know if you have any suggestions.

     

    Thanks