Forum Discussion

Bryan_38092's avatar
Bryan_38092
Icon for Nimbostratus rankNimbostratus
Jun 09, 2015

Create iRule to remove everything after a string found in the URI

Looking for some help, trying to create an iRule to remove everything including a string it finds in the URI. For example, http://www.foobar.com/data/12345=rank&format=XYZ and I want to remove the &format=XYZ so it would be http://www.foobar.com/data/12345=rank

when CLIENT_ACCEPTED {
    set default_pool [LB::server pool]
}

when HTTP_REQUEST {
   set uri [HTTP::uri]
   if { $uri contains "&format=XYZ" } {
      HTTP::uri [string range $uri 11 end]
      pool $default_pool
   }
}
  • this is just another example.

     configuration
    
    [root@ve11b:Active:In Sync] config  tmsh list ltm rule qux
    ltm rule qux {
        when HTTP_REQUEST {
      set uri [HTTP::uri]
      if { $uri contains "format=XYZ" } {
        HTTP::uri [string map {"&format=XYZ" "" "format=XYZ&" "" "format=XYZ" ""} $uri]
      }
      log local0. "Before: $uri After: [HTTP::uri]"
    }
    }
    
     /var/log/ltm
    
    [root@ve11b:Active:In Sync] config  tail -f /var/log/ltm
    Jun 10 22:03:00 ve11b info tmm[10343]: Rule /Common/qux : Before: /data/ After: /data/
    Jun 10 22:03:09 ve11b info tmm[10343]: Rule /Common/qux : Before: /data/12345=rank&format=XYZ After: /data/12345=rank
    Jun 10 22:03:17 ve11b info tmm1[10343]: Rule /Common/qux : Before: /data/format=XYZ&12345=rank After: /data/12345=rank
    Jun 10 22:03:27 ve11b info tmm[10343]: Rule /Common/qux : Before: /data/test=abc&format=XYZ&12345=rank After: /data/test=abc&12345=rank
    
  • Hi Bryan,

    In my example, I considered that the parameter may be in the beginning, case insensitive

    And I think that the pool statement is useless in that scenario.
    when HTTP_REQUEST {
        if { [string tolower [HTTP::query]] starts_with "format=xyz" } {
            HTTP::uri ""
        } elseif { [string tolower [HTTP::query]] contains "&format=xyz" } {
            HTTP::uri [string range [HTTP::uri] 0 [expr "[string first "&format=xyz" [string tolower [HTTP::uri]]]-1"]]
        }
        log local0. "[HTTP::uri]" 
    }
    

    I hope it helps you. [ ]

  • this is just another example.

     configuration
    
    [root@ve11b:Active:In Sync] config  tmsh list ltm rule qux
    ltm rule qux {
        when HTTP_REQUEST {
      set uri [HTTP::uri]
      if { $uri contains "format=XYZ" } {
        HTTP::uri [string map {"&format=XYZ" "" "format=XYZ&" "" "format=XYZ" ""} $uri]
      }
      log local0. "Before: $uri After: [HTTP::uri]"
    }
    }
    
     /var/log/ltm
    
    [root@ve11b:Active:In Sync] config  tail -f /var/log/ltm
    Jun 10 22:03:00 ve11b info tmm[10343]: Rule /Common/qux : Before: /data/ After: /data/
    Jun 10 22:03:09 ve11b info tmm[10343]: Rule /Common/qux : Before: /data/12345=rank&format=XYZ After: /data/12345=rank
    Jun 10 22:03:17 ve11b info tmm1[10343]: Rule /Common/qux : Before: /data/format=XYZ&12345=rank After: /data/12345=rank
    Jun 10 22:03:27 ve11b info tmm[10343]: Rule /Common/qux : Before: /data/test=abc&format=XYZ&12345=rank After: /data/test=abc&12345=rank
    
  • this is just another example.

     configuration
    
    [root@ve11b:Active:In Sync] config  tmsh list ltm rule qux
    ltm rule qux {
        when HTTP_REQUEST {
      set uri [HTTP::uri]
      if { $uri contains "format=XYZ" } {
        HTTP::uri [string map {"&format=XYZ" "" "format=XYZ&" "" "format=XYZ" ""} $uri]
      }
      log local0. "Before: $uri After: [HTTP::uri]"
    }
    }
    
     /var/log/ltm
    
    [root@ve11b:Active:In Sync] config  tail -f /var/log/ltm
    Jun 10 22:03:00 ve11b info tmm[10343]: Rule /Common/qux : Before: /data/ After: /data/
    Jun 10 22:03:09 ve11b info tmm[10343]: Rule /Common/qux : Before: /data/12345=rank&format=XYZ After: /data/12345=rank
    Jun 10 22:03:17 ve11b info tmm1[10343]: Rule /Common/qux : Before: /data/format=XYZ&12345=rank After: /data/12345=rank
    Jun 10 22:03:27 ve11b info tmm[10343]: Rule /Common/qux : Before: /data/test=abc&format=XYZ&12345=rank After: /data/test=abc&12345=rank