Forum Discussion

walt_97468's avatar
walt_97468
Icon for Nimbostratus rankNimbostratus
Dec 02, 2008

302 redirect string capture and replace

I have modified a previous post for this irule:

 

 

when HTTP_REQUEST {

 

HTTP::header replace "Host" "www.test.com"

 

}

 

when HTTP_RESPONSE {

 

HTTP::header replace "Host" "www.test2.com"

 

if { [HTTP::status] starts_with "3"} {

 

if { [HTTP::header location] contains "http://www.redirect.com/" } {

 

set newLoc [string map {www.redirect.com www.test2.com} [HTTP::header location] ]

 

HTTP::header replace location "$newLoc"

 

}

 

}

 

}

 

 

 

We are replacing the host for both requests and responses. Also,when a server sends a 302 response (and I also want to look for 301's), the response includes a location field. A packet capture shows that the loaction field is set to "http://www.redirect.com"

 

 

All I need to do is to change www.redirect.com to www.test2.com, and leave the rest of the URI intact. Am I going about this the right way?

 

 

Thanks for your help!

 

6 Replies

  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    That should work okay. You could replace [HTTP::status] starts_with "3" with HTTP::is_redirect. Also, the Host header does not exist in responses, so you don't need to try to replace/insert it.

     
     when HTTP_REQUEST { 
         Replace the existing host header value with www.test.com 
        HTTP::header replace "Host" "www.test.com" 
     } 
     when HTTP_RESPONSE { 
      
         Check if response is a redirect 
        if { [HTTP::is_redirect]} { 
      
            Check if response contains the old domain name 
           if { [HTTP::header location] contains "http://www.redirect.com/" } { 
      
               Replace the old domain name with the new one in the Location header 
              HTTP::header replace location [string map {www.redirect.com www.test2.com} [HTTP::header location]] 
           } 
        } 
     } 
     

    Aaron
  • It's not rewriting it to www.test2.com.. We are still being redirected to www.redirect.com.

     

     

    Would anyone have any ideas?
  • is there a way to change this using irules?

     

     

     

    To

     

     

     

     

    I've tried using a streams profile and the hosts are reqritten correctly on the base href tag, but the page no longer renders fully, making me think I am breaking a session variable possibly with the rewrite.

     

     

    Thanks!
  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    Hi waltrich,

     

     

    If you create a custom HTTP profile with chunking set to rechunk, LTM will update the content length and the browser will display the full response correctly.

     

     

    If you need to rewrite the response content you can use a stream profile. But it would be better to use an iRule to configure the stream profile. This way you can explicitly enable the stream profile only on text responses (not requests and not binary responses). There are some examples on the STREAM::expression wiki page (Click here).

     

     

    Aaron
  • We fuinally resolved this with an http profile set to rechunk, a stream profile that is actually doing the rewrite and the following irule as this is the only way we were able to rewrite the content in the redirect:

     

     

    when HTTP_REQUEST {

     

    change host header

     

    HTTP::header replace "Host" "www.test.com"

     

    }

     

     

     

    when HTTP_RESPONSE {

     

    HTTP::header replace "Host" "www.test2.com"

     

    if {[HTTP::is_redirect] && [HTTP::header location] contains "www.redirect.com"} {

     

    set oldLoc [HTTP::header Location]

     

    set newLoc1 [string trimleft $oldLoc http://www.redirect.com]

     

    set newLoc [concat https://www.test2.com/$newLoc1]

     

    HTTP::header replace Location $newLoc

     

    }

     

    }

     

     

    We used extensive logging to determine when we had the correct URL and URI. It was a tough one. Thanks for everyone's assistance.
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    I'm glad you got it sorted out,and thanks much for sharing the final solution.

     

     

    Colin