# Replacing Headers using iRules

**URL:** <https://community.f5.com/t/replacing-headers-using-irules/4774>\
**Category:** Technical Forums\
**Tags:** dev, irules, devops, application-delivery\
**Created:** [January 13, 2006, 2:34pm UTC](https://community.f5.com/t/replacing-headers-using-irules/4774 "2006-01-13T14:34:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rluhrman\_127985](https://avatars.discourse-cdn.com/v4/letter/r/a6a055/32.png) [@rluhrman\_127985](https://community.f5.com/u/rluhrman_127985)\
**Post date:** [January 13, 2006, 2:34pm UTC](https://community.f5.com/t/replacing-headers-using-irules/4774/1 "2006-01-13T14:34:09Z")

</div>

I came across an issue where using “replace” did not replace the header with the new header.

Here is an example:

rule WL-Proxy-Client-IP-BAD {

when HTTP\_REQUEST {

if { [HTTP::header exists “X-Forwarded-For”] } {

HTTP::header replace “X-Forwarded-For” “WL-Proxy-Client-IP”

}

}

}

My question is: since the iRule was accepted by BigIP, is the syntax correct and this does not work or is the syntax incorrect?

I found a work around:

rule WL-Proxy-Client-IP {

when HTTP\_REQUEST {

if { [HTTP::header exists X-Forwarded-For] } {

HTTP::header remove X-Forwarded-For

HTTP::header insert WL-Proxy-Client-IP [IP::client\_addr]

}

}

}

I would like to understand why the code above with replace did not work.

Thanks.

RL

---

<div class="post-metadata">

**Author:** ![Colin\_Walker\_12](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@Colin\_Walker\_12](https://community.f5.com/u/Colin_Walker_12)\
**Post date:** [January 16, 2006, 5:26pm UTC](https://community.f5.com/t/replacing-headers-using-irules/4774/2 "2006-01-16T17:26:08Z")

</div>

Well, given the examples you listed, it looks as though you may be looking for different functionality than what the HTTP::header replace command gives.

This command will replace the last instance of whatever header you name, with the contents provided. Note that the header name will remain the **same**.

This means, given your example:

```tcl
when HTTP_REQUEST {
  if { [HTTP::header exists "X-Forwarded-For"] } {
    HTTP::header replace "X-Forwarded-For" "WL-Proxy-Client-IP"
  }
}

```

You would end up with a header named “X-Forwarded-For” with a value of “WL-Proxy-Client-IP”. Note that this would be that exact text, not the value of the Proxy-Client-IP, unless the value was stated there instead of the name.

In the working example, below, you are removing the X-Forwarded-For header, and replacing it wwith a DIFFERENT header, named completely differently, with the client’s IP address as its value.

So, to use the replace command to garner similar results, you would do something to this effect:

```tcl
when HTTP_REQUEST {
  if { [HTTP::header exists "X-Forwarded-For"] } {
    HTTP::header replace "X-Forwarded-For" [IP::client_addr]
  }
}

```

This would result in a header named X-Forwarded-For, with a value of the client’s IP address. If you’re looking to change the name of the header, then the second example you gave, using the remove then the insert with a new name, is the correct syntax.

HTH,

-Colin
