Forum Discussion

Pierce_Fortin's avatar
Pierce_Fortin
Icon for Nimbostratus rankNimbostratus
Nov 28, 2017

Best way to rewrite HTTP/ HTTPS response to change URL?

We have a white-label application which doesn't support being hosted inside subdirectories, IE: The application can live at foo.com/ not foo.com/bar. We have a new business requirement for a client which requires that our application lives at foo.com/bar.

 

I think that the best way to get around this is with 2 iRules:

 

  1. An iRule that rewrites requests to foo.com/bar/stuff to foo.com/stuff. This I have completed.

     

  2. Another iRule which rewrites payloads rewriting any instance of foo.com/stuff to foo.com/bar/stuff.

     

The second iRule is where I'm having trouble, I see examples of doing payload rewrites but not with a regex style match, IE it seems very easy to rewrite foo.com/stuff1 to foo.com/bar/stuff but not for foo.com/* to foo.com/bar/* Ideally, I'd also like to be able to limit this iRule to only inspecting and modifying payloads when an HTTP_REQUEST matches an array of domains to reduce the overall load on F5 but I don't understand how to link a stream rewrite to an HTTP_REQUEST.

 

Any advice is greatly appreciated.

 

2 Replies

  • I think I found part of the answer,

    when HTTP_REQUEST {
       STREAM::disable
       HTTP::header remove "Accept-Encoding"
    }
    when HTTP_RESPONSE {
       if {[HTTP::header value Content-Type] contains "text"}{
          STREAM::expression {@foo.com@foo.com/bar@}
          STREAM::enable
       }
    }
    

    But, I only want the script to run when the HTTP_RESPONSE is a response to a specific set of domains. I'm also unsure how I can not do the STREAM expression when the URL is already foo.com/bar, IE: foo.com/stuff should rewrite to foo.com/bar/stuff but foo.com/bar/other should not rewrite to foo.com/bar/bar/other

  • The key to this is saving info from the Request so you can use it in the Response. Things like the HTTP Host and requested URI are lost on response, example:

    when HTTP_REQUEST {
      set host [HTTP::host]
      set uri [HTTP::uri]
    }
    
    when HTTP_RESPONSE {
      if {$uri starts_with "/stuff" && 
    [HTTP::header value Content-Type] contains "text"}{
          STREAM::expression {@foo.com@foo.com/bar@}
          STREAM::enable
        
      }
    }
    

    Not complete iRule but hope it helps.