Forum Discussion

gscholz's avatar
gscholz
Icon for Nimbostratus rankNimbostratus
Nov 19, 2019

Rewriting Location header for various host names, pointing at same new host name

I have an environment where different pools are selected depending on the URI a user tries to access:

example.com/uri1 -> pool1 -> poolmember1 (rewriting example.com to hostname1)

example.com/uri12-> pool2 -> poolmember1 (rewriting example.com to hostname2:1234)

example.com/uri3 -> pool3 -> poolmember3 (rewriting example.com to hostname3:1235)

Rewriting the Host header gets done in an iRule and works fine.

Sometimes the backend servers reply with a Location header using their internal hostnameX. I understand I can use an iRule that has one line for every single host name used in the backend:

when HTTP_RESPONSE {
   if { [HTTP::is_redirect] } {
       HTTP::header replace Location [string map -nocase {hostname1 example.com} [HTTP::header value Location]]
       HTTP::header replace Location [string map -nocase {hostname2:1234 example.com} [HTTP::header value Location]]
       HTTP::header replace Location [string map -nocase {hostname3:1235 example.com} [HTTP::header value Location]]
   }
}

Is there a way to reduce the iRule to one replacement line that basically changes the host name and if required port within all Location headers to the same host name (example.com), without touching the rest of the URI?

  • You can use the datagroup "key-value" pair and utilize an iRule that replaces the matching key with the corresponding value.

  • String map command first argument is a list with even number of items...

     

    • values at even index are search elements (starting from index 0)
    • values at odd index are replace string

     

    so you can use only one command