# irule to modify hostname in http request and http response

**URL:** <https://community.f5.com/t/irule-to-modify-hostname-in-http-request-and-http-response/58256>\
**Category:** Technical Forums\
**Tags:** ltm, application-delivery\
**Created:** [October 22, 2019, 8:22pm UTC](https://community.f5.com/t/irule-to-modify-hostname-in-http-request-and-http-response/58256 "2019-10-22T20:22:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![KG](https://avatars.discourse-cdn.com/v4/letter/k/bc79bd/32.png) [@KG](https://community.f5.com/u/KG)\
**Post date:** [October 22, 2019, 8:22pm UTC](https://community.f5.com/t/irule-to-modify-hostname-in-http-request-and-http-response/58256/1 "2019-10-22T20:22:24Z")

</div>

Hi All,

I have a requirement to modify the hostnames on the LTMs. We have an external app and it’s accessed from the Internet. Clinets will use [https://xyz.com](https://xyz.com), but the load balanced servers in internal network are configured for the host name - [abc.com](http://abc.com). So, we need to modify the hostname from [xyz.com](http://xyz.com) to [abc.com](http://abc.com) when the LTM sends traffic to the load balanced servers. Also, we need to modify the hostname from [abc.com](http://abc.com) to [xyz.com](http://xyz.com) when LTMs sends the return traffic to the client. I am using the following iRule and I would like to know if it will work

when HTTP\_REQUEST {

if { ([string tolower [HTTP::host]] equals “[xyz.com](http://xyz.com)”) }{

# Replace the host header value with [abc.com](http://abc.com)

HTTP::header replace Host “[abc.com](http://abc.com)”

}

}

when HTTP\_RESPONSE {

# Replace the host header value with [xyz.com](http://xyz.com)

HTTP::header replace Host “[xyz.com](http://xyz.com)”

}

I configured the VIP as a standard VIP and SSL termination (client-ssl and server-ssl) is configured. We installed the certificates for [xyz.com](http://xyz.com) on the LTM.

I am also looking at rewrite profiles. Article link - [Configuring the BIG-IP System as a Reverse Proxy Server](https://techdocs.f5.com/kb/en-us/products/big-ip_ltm/manuals/product/ltm-implementations-13-1-0/15.html#guid-62df4df9-a58f-47b0-90e4-a974cf3cd35f) I am not sure if I can implement this using rewrite profile.

Thanks.

---

<div class="post-metadata">

**Author:** ![Simon\_Blakely](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@Simon\_Blakely](https://community.f5.com/u/Simon_Blakely)\
**Post date:** [October 22, 2019, 9:38pm UTC](https://community.f5.com/t/irule-to-modify-hostname-in-http-request-and-http-response/58256/2 "2019-10-22T21:38:23Z")

</div>

The irule will work correctly for requests - the Host header will be changed correctly.

However, you do not need to modify a host header in a response, as Host is a request header and not a response header. What you do need to do is modify any references within the response to correct the host (for example, the Location header in a HTTP 302 Redirect response may contain the hostname). Cookies may also contain a domain attribute that needs to be corrected to ensure that the client returns the cookies appropriately.

So using the Rewrite profile is a better approach - otherwise you will probably need to us a STREAM irule to carry out textual substitution of the domain names, which the rewrite profile automates.

---

<div class="post-metadata">

**Author:** ![KG](https://avatars.discourse-cdn.com/v4/letter/k/bc79bd/32.png) [@KG](https://community.f5.com/u/KG)\
**Post date:** [October 22, 2019, 9:48pm UTC](https://community.f5.com/t/irule-to-modify-hostname-in-http-request-and-http-response/58256/3 "2019-10-22T21:48:49Z")

</div>

Thanks Blakely. I will try using the rewrite profile. So, can I still use the iRule for requests and use the rewrite profile for responses. Or do you recommend using rewrite profile for both requests and responses. Thanks again.

---

<div class="post-metadata">

**Author:** ![sandy16](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@sandy16](https://community.f5.com/u/sandy16)\
**Post date:** [January 11, 2025, 3:20pm UTC](https://community.f5.com/t/irule-to-modify-hostname-in-http-request-and-http-response/58256/4 "2025-01-11T15:20:06Z")

</div>

Hi KG, I am in the same boat. just checking what was the best way of getting this done?
