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 ?

      • Curious1's avatar
        Curious1
        Icon for Cirrus rankCirrus

        Hey, any further help available on this as it has me beat?

         

        The URLs will change in length as there will be different IP's that need to be removed. No pool will be assigned to the VS and the IP shown in the URL will be used to select a target to connect to. The IP in the URL needs to be removed as this URL will be invalid when requesting the resource on the target.

         

        eg1. hxxp://abc.com/10.4.5.100/path/file (remove the IP and keep anything else)

        eg2. hxxp://abc.com/10.50.100.200/path/file

         

        Any assistance will be really appreciated.