Forum Discussion

Hien_Truong's avatar
Sep 19, 2023

Rewrite the post operation with a body.

Hi;

can you help to write the irule to rewrite the post operation (not uri redirect) with a body as below:

For example:      abc-cd.ef.com/abc/v1/jurisdict/configuration rewrite to cd.ef.com/abc/v1/jurisdict/configuration 

other example:       abc-cd.ef.com/abc/v1/search rewrite to cd.ef.com/abc/v1/search

it mean => abc-cd.ef.com/abc/v1/*  rewrite to cd.ef.com/abc/v1/* 

Note: this is rewrite in POST operation, it is not redirect uri.

thanks a lot, i appreciate your help.

  • Hi Kerry/JRalm;

    The issue resolved when i changed pool members from DEX to NGINX (using the same irule) since rewrite host name locate in NGINX. Thanks a lot for your support.

  • Hi Hien_Truong, it looks like you just need to rewrite the host header inbound? If you want this to be fairly dumb, you can do something like:

    when HTTP_REQUEST {
      if { [HTTP::host] == "abc-cd.ef.com" } {
        HTTP::header replace Host "cd.ef.com"
      }
    }

    If it needs to be dynamic, there are multiple options depending on what the patterns would be. I'll assume a pattern of: nnn-cd.ef.com for the following solution:

    when HTTP_REQUEST {
      # assumes that port 80 is used for http or 443 for https
      # if non-std ports, use contains instead of ends_with
      if { [HTTP::host] ends_with "-cd.ef.com" } {
        HTTP::header replace Host [getfield [HTTP::host] "-" 2]
      }
    }

    If this is not the actual pattern of your hosts that you want to manipulate, post back and we can work on an alternative. untested, make sure to do so in a lab.

  • i tried both of your irules using my browser but i get 404 return. it mean somehow it doesn't hit irule when applying it to F5 then F5 send the request to pool members and get 404 error. i also tried the below irule but still not working. i see "get" method in console. Please advise. Thanks

    when HTTP_REQUEST {
    if {[HTTP::method] eq "POST"}{
    HTTP::header replace origin "http://123.abc.com"
    HTTP::header replace referer "http://123.bcd.com"
    } else { return }
    }

    • JRahm's avatar
      JRahm
      Icon for Admin rankAdmin

      maybe try the serverside of the connection to update the host?

      when HTTP_REQUEST_SEND {
          clientside {
              HTTP::header replace Host <your desired host for server-side>
          }
      }
    • Kerry's avatar
      Kerry
      Icon for Altocumulus rankAltocumulus

      if https.   you need to set the SNI values in the SSL profiles or via Irule.   for testing add a log line so you know if the rules it hit and what things are being set to.   also web server logs will show what being passed to it.        there is a few irules examples aroubd on how to log all html headers.  

      "host" is the header you need to changes.    the two you listed are just for reference. 

       

      • we don't need SNI since both domain names are in different VS. i tried all of irule samples but allways get 404 from server. From curl -vks https://abc-cd.ef.com output, i don't see the header changed. to cd.ef.com. i will add log line to debug it. Any more advise why the irule or rewrite profile not working to replace other domain? thanks

  • Hien_Truong - I accepted your answer as the solution. If any of the replies from Kerry and JRahm were part of that solution feel free to accept those as well - it is ok to select more than one.
    If something else should be chosen (other than yours) you can un-select my choice too.

    Thanks for being part of our community.