Forum Discussion

Eric_Frankenfie's avatar
Eric_Frankenfie
Icon for Nimbostratus rankNimbostratus
Oct 07, 2013

URI Rewrite

I have a developer who is working a new version of an application and they would like to perform the following URI rewrite on the F5 so they don't have to deploy duplicate code to 100+ virtual directories:

 

www.xyz.com/vendorName?whatever=1 to www.xyz.com?vendorName&whatever=1

 

Rather than maintaining a data group with each vendorName is it possible to replace '/vendorName?' with '?vendorName&' dynamically where vendorName will be a variable of any length?

 

  • Assuming you know the vendor name variable, you can do something like this:

    set vendor "vendorName";   assign from header or some other input
    if { [HTTP::uri] starts_with "/${vendor}?" } {
      HTTP::uri [string map [list "/${vendor}?" "?${vendor}&"] [HTTP::uri]]
    }
    

    If you want to look for all patterns and of "/?" and replace it, you can do so with something like this

    if { ([HTTP::uri] starts_with "/") && ([HTTP::uri] contains "?") } {
       Find the index of the "?" character
      set idx [string first "?" [HTTP::uri]]
       extract the string from index 1 up to the character before the "?"
      set site [string range [HTTP::uri] 1 [expr $idx - 1]]
       Modify the URI with the string replacement.
      HTTP::uri [string map [list "/${site}?" "?${site}&"] [HTTP::uri]]
    }
    

    Keep in mind that this isn't bulletproof. So make sure the logic fits for all of your URLs.

    -Joe

  • Here's another option

    if { ([HTTP::uri] starts_with "/") && ([HTTP::uri] contains "?") } {
       Find the index of the "?" character
      set idx [string first "?" [HTTP::uri]]
       replace the first character with "?" and the idx'th character with "&"
      HTTP::uri [string replace [string replace [HTTP::uri] 0 0 "?"] $idx $idx "&"]
    }
    

    Have fun!

    -Joe