Forum Discussion

FMA's avatar
FMA
Icon for Nimbostratus rankNimbostratus
Mar 26, 2018

Replace Host part in HTTP::Location

Hello, guys. I'm trying to find a way to perform manipulation with HTTP::header Location.

1. Web-browser requests https://abc.com
2. Web-server replies with HTTP/301 Location: https://xyz.com/{$URI} (URI part might change when browsing)
3. I need to have HTTP::Location modified to https://abc.com/{$URI} (whatever was put in 2)

I was trying to achieve this with getfield with "/" as delimiter to catch URI part, but this gets broken as number of slashes in Location might increase 😕

Thanks!

2 Replies

  • You could try something like this:

    when HTTP_REQUEST {
        set uri [HTTP::uri]
    }
    
    when HTTP_RESPONSE {
        if {([HTTP::is_redirect]) && ([HTTP::header Location] contains "xyz.com" )} {
            HTTP::header replace Location "https://abc.com$uri"
        }
    }
    
  • Hi,

    I recommend to replace absolute URI to relative URI. This rewrite rule may have been useless if the server had responded with relative URI.

    when HTTP_RESPONSE {
        if { [HTTP::header exists Location] && ([URI::host [HTTP::header value Location]] equals "xyz.com")} {
            HTTP::header replace Location [string map {https://xyz.com/ /} [HTTP::header value Location]]
        }
    }