# policy to replace host value

**URL:** <https://community.f5.com/t/policy-to-replace-host-value/55366>\
**Category:** Technical Forums\
**Tags:** big-ip, irules, application-delivery\
**Created:** [September 20, 2020, 8:00am UTC](https://community.f5.com/t/policy-to-replace-host-value/55366 "2020-09-20T08:00:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam10](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Sam10](https://community.f5.com/u/Sam10)\
**Post date:** [September 20, 2020, 8:00am UTC](https://community.f5.com/t/policy-to-replace-host-value/55366/1 "2020-09-20T08:00:22Z")

</div>

I am trying to create a policy to replace the host in the header.

i.e is when users access [myaccount.xyz.com](http://myaccount.xyz.com) F5 need to replace it with [myaccount.abc.com](http://myaccount.abc.com) and send the response to the server . And when server sends the response F5 should again replace the host to [xyz.com](http://xyz.com) and send to client.

I am able to get it working using an irule with wildcard but when it comes to specific url my irule is not working with the response.

when HTTP\_REQUEST {

set external\_hostname [HTTP::host]

set sub\_hostname [substr [HTTP::host] 0 “.xy.com”]

set internal\_hostname $sub\_hostname.abc.com

HTTP::header replace “Host” $internal\_hostname

}

when HTTP\_RESPONSE {

HTTP::header replace Location [string map -nocase “$internal\_hostname $external\_hostname” [HTTP::header Location]]

}

Which specific host below irule is not working when i add http response, it is replacing all the hosts to [myaccount.abc.com](http://myaccount.abc.com) login page.

when RULE\_INIT {

set ::external\_hostname “[myaccount.xyz.com](http://myaccount.xyz.com)”

set ::internal\_hostname “[myaccount.abc.com](http://myaccount.abc.com)”

}

when HTTP\_REQUEST {

HTTP::header replace “Host” $::internal\_hostname

}

when HTTP\_RESPONSE {

HTTP::header replace Location [string map -nocase “$::internal\_hostname $::external\_hostname” [HTTP::header Location]]

}

---

<div class="post-metadata">

**Author:** ![Andrew-F5](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@Andrew-F5](https://community.f5.com/u/Andrew-F5)\
**Post date:** [September 22, 2020, 5:39pm UTC](https://community.f5.com/t/policy-to-replace-host-value/55366/2 "2020-09-22T17:39:04Z")

</div>

It looks like you may be able to accomplish your goal using a rewrite profile, would be more efficient then an iRule. See the article below and reply back with your thoughts.

> **[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-12-1-0/17.html)**

---

<div class="post-metadata">

**Author:** ![Sam10](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Sam10](https://community.f5.com/u/Sam10)\
**Post date:** [September 22, 2020, 6:37pm UTC](https://community.f5.com/t/policy-to-replace-host-value/55366/3 "2020-09-22T18:37:32Z")

</div>

Andrew

I finally got it working with the below irule but trying to add multiple urls in the request and not getting my syntax correct for the http response. Any help appreciate it.

when HTTP\_REQUEST {

set external\_hostname “[tools.xyz.com](http://tools.xyz.com)”

set internal\_hostname “[tools.abc.com](http://tools.abc.com)”

set external\_hostname1 “[test.xyz.com](http://test.xyz.com)”

set internal\_hostname1 “[test.abc.com](http://test.abc.com)”

if { ([HTTP::host] equals $external\_hostname) } {

HTTP::header replace Host $internal\_hostname

}

if&nbsp;{ ([HTTP::host] equals $external\_hostname1) } {

HTTP::header replace Host $internal\_hostname1

}

}

when HTTP\_RESPONSE {

set external\_hostname “[tools.xyz.com](http://tools.xyz.com)”

set internal\_hostname “[tools.abc.com](http://tools.abc.com)”

set external\_hostname1 “[test.xyz.com](http://test.xyz.com)”

set internal\_hostname1 “[test.abc.com](http://test.abc.com)”

log local0. “The location header [HTTP::header Location] on response”

HTTP::header replace Location [string map -nocase “$internal\_hostname $external\_hostname” [HTTP::header Location]]

HTTP::header replace Location [string map -nocase “$internal\_hostname1 $external\_hostname1” [HTTP::header Location]]

}
