Forum Discussion

y-not-me's avatar
y-not-me
Icon for Nimbostratus rankNimbostratus
Sep 26, 2024

Help with an I-rule rewrite

I'm trying to use an I-rule to add the http hostname in the url to the beginning of the uri then rewrite the hostname. Example:

If the client request to a VIP is https://proxy-v2.api-np-c.newapps.com/<somepath>, I want the request to the server pool to be this https://proxy-v2.api-gw-np-a.newapps.com/proxy-v2.api-np-c.newapps.com/<somepath>

I've tried this and it didn't work:

 

when HTTP_REQUEST {
  if {[HTTP::uri] starts_with "/" } {
    HTTP::uri /[HTTP::host][HTTP::uri]
  }
switch -glob [HTTP::host] {
  "*.api-np-c.newapps.com*" {
         set http_host [HTTP::host]
         set startPos [string first .api-np-c $http_host]
         set endPos [expr $startPos + 11]
         set http_host [string replace $http_host $startPos $endPos .api-gw-np-a]
         HTTP::header replace Host "$http_host"
}
  }

}

-------------------------------------------------------------------------------------------------------

After some digging, I plan to try this next:

when HTTP_REQUEST {
  if {[HTTP::uri] starts_with "/" } {
    HTTP::uri /[HTTP::host][HTTP::uri]
  } else {
switch -glob [HTTP::host] {
  "*.api-np-c.newapps.com*" {
         set http_host [HTTP::host]
         set http_host [string map -nocase {".api-np-c" ".api-gw-np-a"} [HTTP::uri]]
         HTTP::header replace Host "$http_host"
}
}
}
}

As you can probably tell, I'm really new working with i-rules. Any help would be greatly appreciated!

 

  • You could use something like this

     

    when HTTP_REQUEST 

        if { [HTTP::host] equals "proxy-v2.api-np-c.newapps.com" } 
        { 
            HTTP::header replace "Host" "proxy-v2.api-gw-np-a.newapps.com"
            set uri /proxy-v2.api-np-c.newapps.com[HTTP::uri]
            HTTP::uri $uri
        }
    }

     

    Keep in mind that depending on how your web server is configured, you might also need to rewrite responses.

  • Thanks for the response. I needed it to work with *.api-np-c.newapps.com and *api-gw-np-a.newapps.com. I got it working with this, finally. I had the endPos wrong initially. It needed to be +8 not +11:

     

    when HTTP_REQUEST {
      if {[HTTP::uri] starts_with "/" } {
        HTTP::uri /[HTTP::host][HTTP::uri]
      }
    switch -glob [HTTP::host] {
      "*.api-np-c.regionsapps.com*" {
             set http_host [HTTP::host]
             set startPos [string first .api-np-c $http_host]
             set endPos [expr $startPos + 8]
             set http_host [string replace $http_host $startPos $endPos .api-gw-np-a]
             HTTP::header replace Host "$http_host"
    }
      }

    }