X-Forwarded-Host

I wish to set the Host Header to the first value in the X-Forwarded-Host header. Any suggestions on how to best achieve this.

We wrote the code below but it did not work. Can you please assist in resolving the same?

when HTTP_REQUEST { set xff_list ‘’ set x 0 set newheader ‘’ if {[HTTP::header values X-Forwarded-Host] ne “”} { set xff_list [split [HTTP::header values X-Forwarded-Host] “,”]

newheader::[lindex $xff_list $x] newheader::[string trimleft [$newheader] 1] [HTTP::header replace “Host” [$newheader]]

}

Try this:

when HTTP_REQUEST {
    if { [HTTP::header exists X-Forwarded-Host] } {
        HTTP::header replace Host [string trim [lindex [split [HTTP::header X-Forwarded-Host] ","] 0]]
    }
}

This of course assumes that X-Forwarded-Host contains 2 host names separated by a comma, versus two separate X-Forwarded-Host headers (which might also be possible).