Forum Discussion

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

merging two irules into one efficient one.

Hi All,

I was wondering if there is a way to merge two irules into one effcient one. The first irule checks the host

and if a user is coming in as www.mysite.com, it will rewrite the host header to www.mynewsite.com, and if nothing matches, it should redirect all inbound http request to httpS.

The second irule uses a datagroup string that matches the URI. so if the users puts in

http://www.mynewsite.com/abc/app1 they will get redirected to http://www.mynewsite.com/123/app100

Thanks!

-DM

iRule 1

 

 

when HTTP_REQUEST { Check if the Host is www.mysite.com

if { [string tolower [HTTP::host]] eq "www.mysite.com"} {

Rewrite the Host header to www.mynewsite.com

HTTP::header replace Host "www.mynewsite.com"

} else {

HTTP::redirect "https://[HTTP::host][HTTP::uri]"

}

}

iRule 2

 


when HTTP_REQUEST { 
    look for the second string in the data group and redirect 
   set newURI [findclass [HTTP::uri] $::newURI-Datagroup " "] 
   if { "" ne $newURI } { 
      HTTP::redirect "https://www.mynewsite.com/$newURI" 
    } 
}


  • I was wondering what I am doing wrong here...if you look at the results, I get a "double forward slash" after the redirect completes.

    data group string parameter:

     

    Data-group name:URI-datagroup

     

    /abc/app1 /123/app100

    /abc/app2 /123/app200

    /abc/app3 /123/app300

    /abc/app4 /123/app400

    /abc/app5 /123/app500

    Coming From: http://www.mysite.com/abc/app1

    Redirected To:

    
    when HTTP_REQUEST { 
           look for the second string in the data group 
          set newURI [findclass [HTTP::uri] $::URI-datagroup" "] 
          if { "" ne $newURI } { 
             HTTP::redirect "http://www.mysite.com/$newURI" 
        
       } else { 
    HTTP::redirect "https://[HTTP::host][HTTP::uri]" 
       } 
    }
  • You can avoid this by removing the forward slash between .com and $newURI:

     

     

    HTTP::redirect "http://www.mysite.com$newURI"

     

     

    Aaron
  • Thanks Aaron, I will give that a try...How about merging the two irules into one? Can you

     

    give your expert help on the best way? I have the irules above.

     

     

    Thanks again,

     

    -DM
  • You have three main actions from the two iRules:

     

     

    - rewrite host header from www.mysite.com to www.mynewsite.com

     

    - redirect to https:// with the same host and URI the client requested

     

    - redirect to a new URI based on the datagroup lookup

     

     

    These are all mutually exclusive actions. When do you want these actions taken?

     

     

    Aaron
  • Hi Aaron,

     

     

    These are the actions required.

     

     

    1- rewrite host header from www.mysite.com to www.mynewsite.com

     

    2- redirect to a new URI based on the datagroup lookup

     

     

    and if neither action 1 or 2 does not match, than default is action 3

     

    3- redirect to https:// with the same host and URI the client requested

     

     

    I have tried your suggestion and for some reason i sometime get the correct redirect, but some of the

     

    datagroup string parameter does not work. I know I am using the correct syntax with I put this in the URL.

     

    for example http://www.mysite.com/abc/app4 does not go to http://www.mysite.com/123/app400 as per the matching in the

     

    data group and so on...

     

     

    I have put back the "/$newURI" and it all works correctly, the only issue is the double slasshes as below.

     

     

     

     

    Thanks Aaron!

     

     

    -DM

     

     

     

  • So maybe something like this?

    
    when HTTP_REQUEST {
    
        Check if the Host is www.mysite.com
       if { [string tolower [HTTP::host]] eq "www.mysite.com"} { 
    
           Rewrite the Host header to www.mynewsite.com
          HTTP::header replace Host "www.mynewsite.com" 
    
       } else {
    
           look for the second string in the data group and redirect 
          set newURI [findclass [HTTP::uri] $::newURI-Datagroup " "] 
          if { "" ne $newURI } { 
             HTTP::redirect "https://www.mynewsite.com$newURI" 
          } else {
             HTTP::redirect "https://[HTTP::host][HTTP::uri]" 
          }
       }
    }
    

    If you're having issues with the redirects, can you post an anonymized copy of the datagroup contents from the bigip.conf? Please also note a couple of URIs which do and don't work.

    Aaron
  •  

     

    Hi Aaron,

     

     

    The merge works great, however, I am still getting the "double slashes" after the URI...So for example, http://www.mysite.com/abc/app1 gets correctly

     

    redirected now to https://www.mysite.com//123/app100 -- however if you see there is a dboule slasshes after the "123". I have included the class from the bigip.conf files with the contents changed to match this thread.

     

     

    This is driving me crazy...I think everything is done correctly, but something is not set correctly. If i take the "/" from the

     

    class, it does not work at all...(change the string to "/abc/app1 123/app100" or "abc/app1 /123/app100")

     

     

    Thanks again.

     

    -DM

     

     

    class newURI-Datagroup {

     

    "/abc/app1 /123/app100"

     

    "/abc/app2 /123/app200"

     

    "/abc/app3 /123/app300"

     

    "/abc/app4 /123/app400"

     

    "/abc/app5 /123/app500"

     

  • Yeah, you'll need the leading forward slash on the second field of the datagroup entries. Can you log the mapping in the iRule and reply with the output from /var/log/ltm?

    
    when HTTP_REQUEST {
    
        Check if the Host is www.mysite.com
       if { [string tolower [HTTP::host]] eq "www.mysite.com"} { 
    
           Rewrite the Host header to www.mynewsite.com
          HTTP::header replace Host "www.mynewsite.com" 
    
       } else {
    
           look for the second string in the data group and redirect 
          set newURI [findclass [HTTP::uri] $::newURI-Datagroup " "]
          if { "" ne $newURI } {
             log local0. "Mapped [HTTP::uri] to $newURI"
             HTTP::redirect "https://www.mynewsite.com$newURI" 
          } else {
             HTTP::redirect "https://[HTTP::host][HTTP::uri]" 
          }
       }
    }
    

    Aaron
  • Hi Aaron,

     

     

    Below are the logs in the ltm. As you can see, I have tried it both with removing the "/' from "$neURI" (this does not work at all), to keeping "/$newURI". This at least redirects correctly but adds an extra "/" to www.mysite.com//123/app100" as an example.

     

     

    May 23 18:01:33 tmm tmm[993]: Rule TEST_URI_Matching : Mapped /abc/app1 to /123/app100

     

    May 23 18:01:35 tmm tmm[993]: Rule TEST_URI_Matching : Mapped /abc/app1 to /123/app100

     

     

     

    What seems to be the problem? Can we accomplish this another way? This is killing me and we have a deadline to get this working..All the help from you

     

    is greatly appreciated.

     

     

    Thanks,

     

    -DM
  • Can you email me and we can try to solve this a little quicker? My address is my first name at f5.com.

     

     

    Thanks, Aaron