# iRule to modify http response

**URL:** <https://community.f5.com/t/irule-to-modify-http-response/27288>\
**Category:** Technical Forums\
**Tags:** irules, application-delivery\
**Created:** [May 19, 2017, 3:38am UTC](https://community.f5.com/t/irule-to-modify-http-response/27288 "2017-05-19T03:38:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![RobinsonJA\_2717](https://avatars.discourse-cdn.com/v4/letter/r/96bed5/32.png) [@RobinsonJA\_2717](https://community.f5.com/u/RobinsonJA_2717)\
**Post date:** [May 19, 2017, 3:38am UTC](https://community.f5.com/t/irule-to-modify-http-response/27288/1 "2017-05-19T03:38:45Z")

</div>

Hello all,

Seeking some assistance with modifying a http response from a server back to a client.

Senario is as follows (All web traffic)

- VS\_A answers on 443 and sends to pool members on 9999
- VS\_B answers on 888 and sends to same pool members on 7777
- When browsing to VS\_B, there is a function that is on the site that responds with the public url of VS\_A (ex [https://www.acme.com:9999](https://www.acme.com:9999)) instead of [https://www.acme.com](https://www.acme.com) and hence causes a timeout because the clients are not able to access that port directly.

I am seeking to remove the port but keep all uri information.

Additionaly

Do I need any specific settings for the VS in order for this to work?

I have http-wxff profile I have sslclient profile I have sslserver profile

am I missing anything obvious?

Update… I am trying below

I am attempting to modify a server response back to the client. I am attempting to remove the port from the url the client receives while still perserving the case and uri information.

When attempting this I notice no 302 response (Using firefox) and no location information. Seeking help to get this sorted.

when HTTP\_REQUEST { Disable the stream filter for requests STREAM::disable

```tcl
Remove this header to prevent server from compression response
HTTP::header remove Accept-Encoding

```

} when HTTP\_RESPONSE { set internal\_host “[xxx.yyy.com](http://xxx.yyy.com)” set external\_host “[boy.girl.com](http://boy.girl.com)” Rewrite the Location header for redirects if { [HTTP::header exists Location] }{ HTTP::header replace Location [string map “$internal\_host $external\_host” [HTTP::header Location]] }

```tcl
Rewrite the response content using a stream profile if it is text 
if { [HTTP::header Content-Type] contains "text" } { 

     Set the stream expression with the find/replace strings 
    STREAM::expression "@$internal_host@$external_host@" 

     Enable the stream filter 
    STREAM::enable 
}

```

}

---

<div class="post-metadata">

**Author:** ![Pedro\_Haoa](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/pedro_haoa/32/18380_2.png) [@Pedro\_Haoa](https://community.f5.com/u/Pedro_Haoa)\
**Post date:** [May 19, 2017, 3:56am UTC](https://community.f5.com/t/irule-to-modify-http-response/27288/2 "2017-05-19T03:56:03Z")

</div>

Hi,

Try this iRule:

when HTTP\_RESPONSE { if { [HTTP::status] equals “302” && [HTTP::header Location] contains “:9999/” } { HTTP::header replace Location [string map {“:9999/” “”} [HTTP::header Location]] } }

I hope it helps.

Cheers!

---

<div class="post-metadata">

**Author:** ![JG](https://avatars.discourse-cdn.com/v4/letter/j/dfb087/32.png) [@JG](https://community.f5.com/u/JG)\
**Post date:** [May 19, 2017, 3:56am UTC](https://community.f5.com/t/irule-to-modify-http-response/27288/3 "2017-05-19T03:56:03Z")

</div>

"

```tcl
there is a function that is on the site that responds with the public url of VS_A

```

"

Was this in the form of an HTTP redirect?

---

<div class="post-metadata">

**Author:** ![RobinsonJA\_2717](https://avatars.discourse-cdn.com/v4/letter/r/96bed5/32.png) [@RobinsonJA\_2717](https://community.f5.com/u/RobinsonJA_2717)\
**Post date:** [May 19, 2017, 3:59am UTC](https://community.f5.com/t/irule-to-modify-http-response/27288/4 "2017-05-19T03:59:12Z")

</div>

There is an option that when selection responds with the location on another url on a specific port.

---

<div class="post-metadata">

**Author:** ![JG](https://avatars.discourse-cdn.com/v4/letter/j/dfb087/32.png) [@JG](https://community.f5.com/u/JG)\
**Post date:** [May 19, 2017, 4:04am UTC](https://community.f5.com/t/irule-to-modify-http-response/27288/5 "2017-05-19T04:04:41Z")

</div>

If it is an HTTP redirect, you can edit your HTTP profile and select “All” for “Redirect Rewrite”.

---

<div class="post-metadata">

**Author:** ![JG](https://avatars.discourse-cdn.com/v4/letter/j/dfb087/32.png) [@JG](https://community.f5.com/u/JG)\
**Post date:** [May 19, 2017, 6:07am UTC](https://community.f5.com/t/irule-to-modify-http-response/27288/6 "2017-05-19T06:07:29Z")

</div>

Try this one then:

```tcl
when HTTP_RESPONSE {
    if { [HTTP::header exists "Location"] } {
        log local0. "Original Location: [HTTP::header Location]"                                            
        set new_location [string map {"www.acme.com:9999" "www.acme.com:888"} [HTTP::header value Location]] 
        HTTP::header replace Location $new_location
    }
}

```

---

<div class="post-metadata">

**Author:** ![stan\_piron](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@stan\_piron](https://community.f5.com/u/stan_piron)\
**Post date:** [May 20, 2017, 6:55am UTC](https://community.f5.com/t/irule-to-modify-http-response/27288/7 "2017-05-20T06:55:00Z")

</div>

Hi,

You can try to use rewrite profile instead of irule.

Create a rewrite profile type translation.

Create a rule in this profile with parameters:

- direction : response
- client URL : [https://www.acme.com/](https://www.acme.com/)
- server URL : [https://www.acme.com:9999/](https://www.acme.com:9999/)

Assign this profile to the VS\_B

---

<div class="post-metadata">

**Author:** ![RobinsonJA\_2717](https://avatars.discourse-cdn.com/v4/letter/r/96bed5/32.png) [@RobinsonJA\_2717](https://community.f5.com/u/RobinsonJA_2717)\
**Post date:** [May 20, 2017, 6:46pm UTC](https://community.f5.com/t/irule-to-modify-http-response/27288/8 "2017-05-20T18:46:30Z")

</div>

Able to resolve using the original iRule. Had a tired brain in messing with wrong virtual servers. Thanks all for you assistance.
