Forum Discussion

Lemaire_Frédéri's avatar
Jan 31, 2022

Rewriting multiple values of HTTP Location header

Hello

I'm trying to rewrite a HTTP location header in a response  using a list of possible value to be rewrited

The response to be rewrited :

http://myorighost:81/SASPortal/jsp/html/portal/portal.js

http://myrewritedhost/analytics/Portal/jsp/html/portal/portal.js

So, in the header location, both the host and the path /analytics/portal should be rewrited.

How can I test multiple value to be changed?

I tried this without success:

HTTP::header replace Location [string map -nocase [ list "myorighost:81" "myrewirtedhost" "/analytics/Portal" "/SASPortal/"  "/valuex/" "/valuexrewrited"] [HTTP::header value Location]]

Thanks in advance.

Regards 

Frédéric

 

3 Replies

  • Hello Frédéric, do you need a redirect from first URL to second URL? 

    Will the two URLs always be the same or is there a whole subfolder content to be redirected?

     

    In first case, in my opinion a LTM policy configured for static redirect will just work fine.

    In second case, there's a few ways to do it depending on the requirements.

    If there's a big list of URLs to be handled, you might want to work with Data Groups to keep it simple. Configure a string-type DG like this:

     

     

    ltm data-group internal /Common/datagroup_path_rewrite {
     records {
     /oldsubfolder1/ { }
     /oldsubfolder2/ { }
     /oldsubfolder3/ { }
     /oldsubfolder4/ { }
     /oldsubfolder5/ { }
     }
     type string
    }

     

     

     

    Then use it in an iRule for path replacement

     

     

    when HTTP_REQUEST {
    
      if {[string tolower [HTTP::host]] eq "myorighost:81" } {
        HTTP::header replace Host "myrewritedhost"
        # this is not a redirect
      }
    
      # read as: does the uri contain an element of datagroup_path_rewrite (case sensitive)
      if {[class match [HTTP::uri] contains datagroup_path_rewrite]} {
    
        HTTP::path /analytics/Portal/jsp/html/portal/portal.js
        # this rewrites the URI path before senting traffic to BE server
      }
    }

     

     

    Also, I've recently answered a similar question in this thread, see if this helps as well. 

    • Lemaire_Frédéri's avatar
      Lemaire_Frédéri
      Icon for Altostratus rankAltostratus

      Hello,

      Thanks for your answer.

      In fact, I need to replace a reverse proxy with the F5, currently with LTM.

      The problem I'm having is the rewriting of the RESPONSE and specifically the Location Header.
      For the HTTP request rewriting  I was able to rewrite it with "internalhost" and internal uri/path to the back-end webserver.

      The problem I'm having in the Response redirects coming from the back-end that I need to rewrite with external host + adapt some path and query params ( within the same redirect location )

      How can I do that ?

      for exemple :

      Http location received from the back-end that need to be rewrited to 

      http://internalhost/SASPortal/public http://ExternalHost/analytics/Portal/public

      http://internalhost/SASPortal/test?myparam=http%3ASASPortal...  http://internalhost/SASPortal/test?myparam=http%3%2F%2Fanalytics%2FPortal... 

      http://internalhost/SASTheme/mytheme http://ExternalHost/Theme_extranal/mytheme

      I also need to rewrite the referer with the external uri and some relative path in the location

      For the Content rewriting of the response, I 'm using a stream profile and this is ok . But I'm having a lot of problems with the rewriting of the Location header.


      many thanks in advance for your future answers.

      Regards

      Frédéric

       

       

       

       

       

      • Enes_Afsin_Al's avatar
        Enes_Afsin_Al
        Icon for MVP rankMVP

        Hi Frédéric,

        Can you add default stream profile to vs and try this iRule?

         

        when HTTP_REQUEST {
        	STREAM::disable
        	if { [HTTP::host] eq "externalhost" } {
        		HTTP::host "internalhost"
        		switch -glob [HTTP::uri] {
        			"/analytics/Portal*" { HTTP::uri [string map {"/analytics/Portal" "/SASPortal"} [HTTP::uri]] }
        			"/Theme_extranal*" { HTTP::uri [string map {"/Theme_extranal" "/SASTheme"} [HTTP::uri]] }
        		}
        	}
        }
        
        when HTTP_RESPONSE {
        	if { [HTTP::header exists "Location"] } {
        		HTTP::header replace Location [string map [list "internalhost" "externalhost" "/SASPortal" "/analytics/Portal" "/SASTheme" "/Theme_extranal" "/valuefrominternal/" "/valuetoexternal/"] [URI::decode [HTTP::header Location]]]
        	}
        	STREAM::expression {@internalhost@externalhost@}
        	STREAM::enable
        }