Forum Discussion

pgermain_71805's avatar
pgermain_71805
Icon for Nimbostratus rankNimbostratus
Aug 05, 2009

URI Rewrite Help

We want to take all requests from a particular IP to a particular URI, and add an additional directory to the URI. We do not want to redirect.

 

 

For example, if a request from 2.2.2.2 comes in to /SOAPAPI, I want to rewrite the URI to /SOAPAPI/Test

 

So far, the iRule I created hasn't worked.

 

when HTTP_REQUEST {

 

if { ([IP::client_addr] equals "2.2.2.2") and ([HTTP::uri] starts_with "/SOAPAPI") } {

 

HTTP::uri "/SOAPAPI/Test[string range $uri 8 end]"

 

}

 

}

 

 

I lifted this example from DevCentral and I didn't get an error when I created the rule.

 

 

The F5 sends a reset when I test it and it doesn't pass the traffic to the inside.

 

 

If anybody has some working rules that do the same thing or sees something I am doing wrong, please let me know.

7 Replies

  • I believe that the reason why you're getting a reset is that you're referencing a variable that was never set: the $uri variable. Try setting that variable first

      
     set uri [HTTP::uri]  
     

    to get rid of the resets, then add some logging to see what is going on. You're on the right track...

    -Matt
  • Thanks Matt,

     

     

    I have it working now with [HTTP::uri], plus some other ideas I lifted from other posts...

     

     

    when HTTP_REQUEST {

     

     

    start debug

     

    set testuri [HTTP::uri]

     

    log local0.debug "> ****StartURI='$testuri'";

     

    end debug

     

    if { ([IP::client_addr] equals "2.2.2.2") and ([HTTP::uri] starts_with "/SOAPAPI") } {

     

    set variables

     

    set origUri

     

    set origString "/SOAPAPI";

     

    set newString "/SOAPAPI/test";

     

    regsub -all $origString $origUri $newString newUri

     

    start debug

     

    set testuri [HTTP::uri]

     

    log local0.debug "> ****StartURI='$testuri'";

     

    log local0.debug "> ****EndNewURI='$newUri'";

     

    end debug

     

    write the new URI to HTTP session

     

    HTTP::uri $newUri

     

    string replace

     

    }

     

     

    }

     

     

    Much Appreciated,

     

     

    Paul
  • Good work! Just as a suggestion, you may consider using string map instead of regsub, as it's way more efficient and CPU friendly. Here's a recent example of its use for a very similar task: http://devcentral.f5.com/Default.aspx?tabid=53&forumid=5&postid=60029&view=topic

     

     

    -Matt
  • That is the very example I lifted it from. I'm not familiar enough with the scripts to take the original scripts (with regsub) and make the changes suggested in the followup posts.

     

     

    I'll work at rewriting it.

     

     

    Paul

     

  • Also, the suggestion in that post uses a redirect instead of a rewrite. I'll search some more on string map.
  • I think something like this may work for you (untested, syntax or otherwise):

     
     set newURI [string map {"/SOAPAPI" "/SOAPAPI/test"} [HTTP::uri]]  
     

    Good luck!

    -Matt
  • Matt,

     

     

    You are a master. I used that and removed the debugging commands for a short and sweet script.

     

     

    when HTTP_REQUEST {

     

    if { ([IP::addr [IP::client_addr]/28 equals 216.57.132.112]) and ([HTTP::uri] starts_with "/SOAPAPI") } {

     

    set variables

     

    set newUri [string map {"/SOAPAPI" "/SOAPAPI/barclays"} [HTTP::uri]]

     

    write the new URI to HTTP session

     

    HTTP::uri $newUri

     

    string replace

     

    }

     

     

    }

     

     

    Thanks A Million,

     

     

    Paul