Forum Discussion

Jason_Roppolo_3's avatar
Jason_Roppolo_3
Historic F5 Account
Apr 21, 2006

http redirect and port stripping

All,

 

 

Can anyone assist me with an iRule that would strip a certain string out of a url?

 

 

 

The customer has a vip that is 10.0.0.2:8010 which does a redirect to https://[HTTP::host][HTTP::uri] using the following:

 

 

 

when HTTP_REQUEST {

 

HTTP::redirect "https://[HTTP::host][HTTP::uri]

 

 

 

The only issue with this is that it redirects to https://foo.com:8010/mysupersecreturi/blammo

 

 

I can't do a hard coded redirect because they are using http headers on the back end web server so don't even try it!

 

 

 

I think I am missing something pretty simple but after 12 hours of GTM installation my brain is mush!

 

 

 

Thanks for your help...

 

  • Your assumption is correct.

     

    The following is captured in the log:

     

     

    ltm:Apr 25 01:11:31 tmm tmm[1304]: Rule RedirectToSecureCgpAdmin : HTTP-host is csr.candwall.com:8010

     

    ltm:Apr 25 01:11:31 tmm tmm[1304]: Rule RedirectToSecureCgpAdmin : HTTP-host is csr.candwall.com:8010
  • Try this:

    
    when HTTP_REQUEST {
      if { [HTTP::host] ends_with ":8010" } {
        set http_host [string trimright [HTTP::host] ":"]
        HTTP::redirect "https://$http_host[HTTP::uri]
      }
    }

  • Thanks for your response.

     

     

    However, it did not work. The redirection was still to

     

    https//csr.candwall.com:8010/. The 8010 was not stripped.

     

     

    Is there documentation on trimright?

     

    What version of LTM code is required for "trimright" to work?
  • The "string trimright" command is a internal TCL command that is supported in our iRules language. Here's the TCL reference site and the link to the usage of the "string" command.

     

     

    http://tmml.sourceforge.net/doc/tcl/index.html Click here

     

    http://tmml.sourceforge.net/doc/tcl/string.html Click here

     

     

    Citizen had one thing wrong with the trimright subcommand. It will trim the supplied characters from the end of the string, not all characters after the supplied string. If you know the port will be 8010, you could hardcode it like this

     

     

    when HTTP_REQUEST {
      if { [HTTP::host] ends_with ":8010" } {
        set http_host [string trimright [HTTP::host] ":8010"]
        HTTP::redirect "https://$http_host[HTTP::uri]"
      }
    }

     

     

    Or you could just omit the first ends_with check as the trimright will return the entire string if the supplied matching string isn't found

     

     

    when HTTP_REQUEST {
      HTTP::redirect "https://[string trimright [HTTP::host] ":8010"][HTTP::uri]"
    }

     

     

    But, if you don't know what the port will be, you could write it more genericly like this:

     

     

    when HTTP_REQUEST {
      set http_host [HTTP::host]
       look for last occurance of ":" in HTTP::host
      set colon [string last ":" [HTTP::host]]
      if { $colon != -1 } {
         if a colon is found, then extract characters 0 up until 1 before
         the colon
        set http_host [string range [HTTP::host] 0 [expr $colon - 1]]
      }
      HTTP::redirect "https://$http_host[HTTP::uri]"
    }

     

     

    It's less overhead to hard code it, but you have options either way.

     

     

    -Joe
  • Deb_Allen_18's avatar
    Deb_Allen_18
    Historic F5 Account
    Not to say your algorithm isn't the prettiest baby I ever saw, Joe, but since there can only be one : in a host header, I just use this:

    
    [getfield [HTTP::hostname] ":" 1]

    Either way it returns the hostname without port...

  • Jason_Roppolo_3's avatar
    Jason_Roppolo_3
    Historic F5 Account
    I think Deb Allen wins this one with the most elegant and simplest.....

     

     

    Customer is working and happy!