# Dynamic set-cookie value on redirect

**URL:** <https://discourse.haproxy.org/t/dynamic-set-cookie-value-on-redirect/918>\
**Category:** Help!\
**Created:** [January 6, 2017, 10:17am UTC](https://discourse.haproxy.org/t/dynamic-set-cookie-value-on-redirect/918 "2017-01-06T10:17:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fayizk1](https://avatars.discourse-cdn.com/v4/letter/f/a88e4f/32.png) [@fayizk1](https://discourse.haproxy.org/u/fayizk1)\
**Post date:** [January 6, 2017, 10:17am UTC](https://discourse.haproxy.org/t/dynamic-set-cookie-value-on-redirect/918/1 "2017-01-06T10:17:04Z")

</div>

I want to set dynamic cookie value(from rand function) while doing re-direction. First i tried `set-cookie` option like following:

> http-request redirect code 307 location %[capture.req.uri] set-cookie test=%[rand(20)]

but this gives me:

> HTTP/1.1 307 Temporary Redirect  
> Cache-Control: no-cache  
> Content-length: 0  
> Location: /test  
> Set-Cookie: test=%[rand(20)]; path=/;

after reading haproxy source code i found a workaround, adding `Set-Cookie` header after location with `\r\n` works perfectly.

> http-request redirect code 307 location %[capture.req.uri]\r\nSet-Cookie:\ test=%[rand(20)]

output:

> HTTP/1.1 307 Temporary Redirect  
> Cache-Control: no-cache  
> Content-length: 0  
> Location: /test  
> Set-Cookie: test=16

I want to know whether this is a good practice?.

---

<div class="post-metadata">

**Author:** ![lucid\_thayne](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/lucid_thayne/32/660_2.png) [@lucid\_thayne](https://discourse.haproxy.org/u/lucid_thayne)\
**Post date:** [December 21, 2018, 6:39pm UTC](https://discourse.haproxy.org/t/dynamic-set-cookie-value-on-redirect/918/2 "2018-12-21T18:39:27Z")

</div>

I need this as well.

At first I thought I could use a `http-request add-header` command to do this, but that doesn’t work. After finding this: [https://www.mail-archive.com/haproxy@formilux.org/msg25061.html](https://www.mail-archive.com/haproxy@formilux.org/msg25061.html) I think your hack is the only way to do it. Although I definitely think it should have a way to add headers using log-format syntax.

---

<div class="post-metadata">

**Author:** ![happy](https://avatars.discourse-cdn.com/v4/letter/h/f05b48/32.png) [@happy](https://discourse.haproxy.org/u/happy)\
**Post date:** [December 26, 2018, 4:46am UTC](https://discourse.haproxy.org/t/dynamic-set-cookie-value-on-redirect/918/3 "2018-12-26T04:46:36Z")

</div>

Lua way

Cfg:  
set your cookie and location headers  
…  
use\_backend redirectWithSetCookie  
…  
backend redirectWithSetCookie  
http-request use-service lua.redirectWithSetCookie

Lua:  
core.register\_service(“redirectWithSetCookie”, “http”, function(applet)  
applet:set\_status(301)  
applet:add\_header(“set-cookie”, applet.headers[‘set-cookie’][0])  
applet:add\_header(“location”, applet.headers[‘location’][0])  
applet:start\_response()  
applet:send(’’)  
end)

---

<div class="post-metadata">

**Author:** ![JeroenVermeulen](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/jeroenvermeulen/32/1137_2.png) [@JeroenVermeulen](https://discourse.haproxy.org/u/JeroenVermeulen)\
**Post date:** [April 16, 2020, 2:02pm UTC](https://discourse.haproxy.org/t/dynamic-set-cookie-value-on-redirect/918/4 "2020-04-16T14:02:10Z")

</div>

Based on @happy’s idea I was able to do it easier in HA-Proxy v2.0.13 with Lua 5.3.3:

File **redirect.lua** :

```
core.register_service("RedirectAllHeaders", "http", function(applet)
    applet:set_status(302)
    applet:start_response()
    applet:send("")
end)

```

**HAProxy config:**

```
global
    lua-load /path/to/redirect.lua

backend bk_redir
    http-request use-service lua.RedirectAllHeaders
    http-response add-header Set-Cookie redir=1;\ path=/
    http-response add-header X-LB-Backend bk_redir
    http-response add-header Location http://nowherefast.com/

frontend fr_http_https
    bind 11.22.33.44:80
    default_backend bk_redir
```
