Forum Discussion

Curious1's avatar
Curious1
Icon for Cirrus rankCirrus
Nov 29, 2019

URI Rewrite remove info

How can I remove info from the below URI? Other objects will be called after the numbers so cant just rewrite URI to "/"

 

hxxp://abc.com/1.2.3.4 : remove any IP address after the /

  • Hi Curious,

    Can you try this iRule?

    when HTTP_REQUEST {
        if { [string match {/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*} [HTTP::uri]] } {
            # log local0. "matchedUri: [HTTP::uri]"
            
            set changedUri [string trimleft [HTTP::uri] "/"]
            # log local0. "changedUri: $changedUri"
            
            if { $changedUri contains "/"} {
                set newUri "[string range $changedUri [string first / $changedUri] end]"
            } else {
                set newUri "/"
            }
            # log local0. "newUri: $newUri"
            
            HTTP::redirect "http://[HTTP::host]$newUri"
        }
    }

    You can change regex with better expression. I couldn't find exact expression for the IP match.

  • Hi Curious,

    Can you try this iRule?

    when HTTP_REQUEST {
        if { [string match {/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*} [HTTP::uri]] } {
            # log local0. "matchedUri: [HTTP::uri]"
            
            set changedUri [string trimleft [HTTP::uri] "/"]
            # log local0. "changedUri: $changedUri"
            
            if { $changedUri contains "/"} {
                set newUri "[string range $changedUri [string first / $changedUri] end]"
            } else {
                set newUri "/"
            }
            # log local0. "newUri: $newUri"
            
            HTTP::redirect "http://[HTTP::host]$newUri"
        }
    }

    You can change regex with better expression. I couldn't find exact expression for the IP match.

  • use a string replace function, you can find many answers

     

    https://devcentral.f5.com/s/question/0D51T00006i7k6Z/irule-replace-a-part-of-uri

     

    https://devcentral.f5.com/s/articles/irules-101-14-tcl-string-commands-part-2

    • string tcl can look for exact strings, it doesn't act as a regex. The Op is asking for any IP address, not a particular IP address. Would this be still possible ?

      • boneyard's avatar
        boneyard
        Icon for MVP rankMVP

        if you first get the ip address, split on / perhaps you can string replace against that i guess.

         

        also with a other string tricks you can make this work. for example look for first slash, remove (trim) upto that.

         

        and of course you could use regexp as you point out, although the performance impact of that is higher.