Forum Discussion
Wes_98712
Nimbostratus
Mar 30, 2007rewrite HTML in response
Interesting dilemma we have here. We are about to release a new version of a site we own, but don't have time to change the HTML on a particular page, essentially the hyperlink points to site that will be going away after launch. So, what I want to do, is look for the hyperlink in the response and replace it with something else. I am not even sure if it is possible.
Right now the page links to a particular URL, upon response from any server, I want to replace www.foo.com/someuri with www.foofoo.com/somenewuri.
I thought HTTP_RESPONSE would be a good place to start, but I have a feeling I am going to have to inspect the body in order to do this, and I am really not sure what performance impact this will have.
I have a couple of options that I am playing with, 1 basically transfers the domain to us, then we handle requests as a redirect to the new destination, and of course replace the links in the code...course that all makes sense, but timing is critical, so if I can put this in place I'll be gold.
Not sure where to start with this, since it deals more with the body of the response rather than the header.
All comments would be appreciated.
- Wes_98712
Nimbostratus
BTW I had thought the following would work, but no dice, it doesn't find anything, so I must be misinterpreting the function of the regsub or possibly the HTTP::payloadwhen HTTP_RESPONSE { set old_link "www.somesite.com/someuri" set new_link "host.somesite.com/somenewuri" if { [regsub -all {$old_link} [HTTP::payload] $new_link newdata] } { log "Replacing $old_link with $new_link" HTTP::payload replace 0 [HTTP::payload length] $newdata HTTP::release } else { log "replacing nothing" } }
- JRahm
Admin
you'd need an HTTP::collect in the HTTP_RESPONSE event, then look at your payload in the HTTP_RESPONSE_DATA event. - Wes_98712
Nimbostratus
Something like as follows right?when HTTP_RESPONSE { if { $scrub_content } { if { [HTTP::header exists "Content-Length"] } { set content_length [HTTP::header "Content-Length"] } else { set content_length 4294967295 } if { $content_length > 0 } { HTTP::collect $content_length } } } when HTTP_RESPONSE_DATA { set old_link "www.somedomain.com/someuri" set new_link "www.somedomain1.com/somenewuri" if { [regsub -all {$old_link} [HTTP::payload] $new_link newdata] } { log "Replacing $old_link with $new_link" HTTP::payload replace 0 [HTTP::payload length] $newdata } else { log "replacing nothing" } }
- This is overlooked most of the time, but if you want a simple string replacement, the easiest solution is to use a Stream Profile associate with your virutal. There are no iRules involved. Just create a stream profile with the Source value as "www.somedomain.com/someuri" and the Target value as "www.somedomain1.com/somenewuri" and apply it to your virtual.
- Wes_98712
Nimbostratus
Ok, so I tried both setting up a stream profile and looking for and replacing the string in question, and it gave me a page cannot be displayed error, I also tried it in a irule as follows:when HTTP_RESPONSE { STREAM::enable STREAM::expression "@careers-24hourfit@24hr.e-selecsys@" }
when HTTP_RESPONSE { STREAM::enable STREAM::expression "@careers@24hr@" }
- Do you have "Response Chunking" set to "Rechunk" in your HTTP Profile? That might be something to check as I believe that needs to be set for the stream profile to work when the response size changes.
- Wes_98712
Nimbostratus
Bammo, that did it, rechunk was the key, it replaces the string perfectly. - Wes_98712
Nimbostratus
Forgot to ask, what if I have multiple static strings in the same page that I want to replace, would I have to have separate streams profiles, or could I reuse?when HTTP_RESPONSE { STREAM::enable STREAM::expression "@http://www.somedomain.com/someuri@https://somehost.domain/somenewuri@ @http://www.someotherdomain.com/someuri@https://somehost.domain/somenewuri@"" }
- BTW, there is a good overview of the Stream Profile iRule components in this thread:
http://devcentral.f5.com/Default.aspx?tabid=53&view=topic&forumid=5&postid=5097
- Wes_98712
Nimbostratus
Yes, we are good to go. The back door comment is also documented in the following Solutions article:https://tech.f5.com/home/solutions/sol7027.htmlAsk F5 Solution 7027
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects