# Insert Cookie for a different domain

**URL:** <https://community.f5.com/t/insert-cookie-for-a-different-domain/2640>\
**Category:** Technical Forums\
**Tags:** irules, dev, devops, application-delivery\
**Created:** [February 22, 2005, 8:38pm UTC](https://community.f5.com/t/insert-cookie-for-a-different-domain/2640 "2005-02-22T20:38:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaron\_Magruder](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@Aaron\_Magruder](https://community.f5.com/u/Aaron_Magruder)\
**Post date:** [February 22, 2005, 8:38pm UTC](https://community.f5.com/t/insert-cookie-for-a-different-domain/2640/1 "2005-02-22T20:38:18Z")

</div>

Is it possible to insert a cookie for domain [xyz.domain.com](http://xyz.domain.com) when a customer visits [abc.domain.com](http://abc.domain.com), then redirect them to [xyz.domain.com](http://xyz.domain.com)?

When the customer gets redirected, I would like to have the BigIP look for the cookie that I inserted when they visited [abc.domain.com](http://abc.domain.com). If the cookie exists, use pool xyz, if not redirect them to [abc.domain.com](http://abc.domain.com) to get a cookie.

Here is what I have come up with so far, but the cookie insertion isn’t working. I am getting redirected back and forth between web sites.

Thanks for any assistance.

VS abc

rule abc\_redirect

VS xyz

rule xyz1

rule abc\_redirect {

when CLIENT\_ACCEPTED {

set ckname xyz

set ckvalue [IP::client\_addr]

set ckdomain [xyz.domain.com](http://xyz.domain.com)

}

when HTTP\_REQUEST {

HTTP::cookie insert name $ckname value $ckvalue path / domain $ckdomain

HTTP::redirect “[https://xyz.domain.com](https://xyz.domain.com)”

}

rule xyz1

when CLIENT\_ACCEPTED {

set ckname xyz

set ckvalue [IP::client\_addr]

set ckdomain [xyz.domain.com](http://xyz.domain.com)

}

when HTTP\_REQUEST {

if {[HTTP::cookie exists $ckname]} {

HTTP::cookie remove $ckname

pool xyz

}

else {

HTTP::redirect “[https://abc.domain.com](https://abc.domain.com)”

}

}

---

<div class="post-metadata">

**Author:** ![bl0ndie\_127134](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@bl0ndie\_127134](https://community.f5.com/u/bl0ndie_127134)\
**Post date:** [February 22, 2005, 10:06pm UTC](https://community.f5.com/t/insert-cookie-for-a-different-domain/2640/2 "2005-02-22T22:06:04Z")

</div>

Are you seeing the Set-Cookie being sent back as a response on the wire? I suspect that your user agent might be rejecting the cookies because the host name of the Set-Cookie is different that the current host name. In another words, I don’t think you are allowed to do a Set-Cookie [abc.domain.com](http://abc.domain.com) from [123.domain.com](http://123.domain.com).

Its hard to tell what you are trying to do exactly, but would it be possible to put information about the domains that you visited inside the cookies instead of playing games with the domain values? If so, you should set the domain to “.domain.com.”. Hope that helps.

---

<div class="post-metadata">

**Author:** ![Aaron\_Magruder](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@Aaron\_Magruder](https://community.f5.com/u/Aaron_Magruder)\
**Post date:** [February 22, 2005, 11:58pm UTC](https://community.f5.com/t/insert-cookie-for-a-different-domain/2640/3 "2005-02-22T23:58:49Z")

</div>

No I don’t see the cookie being sent back. It doesn’t appear that my syntax is correct or that the BigIP won’t send a cookie for a different domain.

To give more detail on our issue:

We have two 3DNS controllers and two pairs of BigIP’s in two datacenters. We need to persist customers to one datacenter. Our TTL’s on the wide IP’s are 30 seconds to provide fast failover in the event of a datacenter failure. We use LDNS persistence on the 3dns. This works great except for the ISP who load balancer their dns servers. Each ldns server may get a persisted to a different datacenter. When the user queries their ldns, they are getting different answers and getting forwarded to different datacenters.

We would like the user to go to [aac.domain.com](http://aac.domain.com), get a cookie for [aac1.domain.com](http://aac1.domain.com) or [aac2.domain.com](http://aac2.domain.com), depending on what datacenter they are in. Once they get the cookie, they will be redirected to either aac1 or aac2, depending on what datacenter they are in. When the contact aac1 or [aac2.domain.com](http://aac2.domain.com), the BigIP will check to see if a cookie is present. If not, they will be redirected to [aac.domain.com](http://aac.domain.com).

This is an attempt to keep customers from book marking aac1 or [aac2.domain.com](http://aac2.domain.com). If they bookmark on a datacenter, load balancing is diminished or that datacenter may not be available.

Any idea if the BigIP will allow you to inject a cookie for a domain that you are not currently visiting?

---

<div class="post-metadata">

**Author:** ![rapmaster\_c\_127](https://avatars.discourse-cdn.com/v4/letter/r/6de8d8/32.png) [@rapmaster\_c\_127](https://community.f5.com/u/rapmaster_c_127)\
**Post date:** [February 23, 2005, 7:12am UTC](https://community.f5.com/t/insert-cookie-for-a-different-domain/2640/4 "2005-02-23T07:12:17Z")

</div>

Perhaps you mean to latch the HTTP\_RESPONSE event instead of HTTP\_REQUEST?

---

<div class="post-metadata">

**Author:** ![Aaron\_Magruder](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@Aaron\_Magruder](https://community.f5.com/u/Aaron_Magruder)\
**Post date:** [February 23, 2005, 1:00pm UTC](https://community.f5.com/t/insert-cookie-for-a-different-domain/2640/5 "2005-02-23T13:00:17Z")

</div>

I have tried that too. I don’t see the cookie being injected during a trace. I tried inserting a cookie by the persist and HTTP::cookie insert methods.

when CLIENT\_ACCEPTED {

set ckname acc1

set ckvalue [IP::client\_addr]

set ckdomain [acc1.domain.com](http://acc1.domain.com)

}

when HTTP\_RESPONSE {

persist cookie insert [acc1.domain.com](http://acc1.domain.com)

HTTP::cookie insert name $ckname value $ckvalue path / domain $ckdomain

HTTP::redirect “[http://acc1.domain.com](http://acc1.domain.com)”

}

---

<div class="post-metadata">

**Author:** ![drteeth\_127330](https://avatars.discourse-cdn.com/v4/letter/d/cc9497/32.png) [@drteeth\_127330](https://community.f5.com/u/drteeth_127330)\
**Post date:** [February 23, 2005, 4:25pm UTC](https://community.f5.com/t/insert-cookie-for-a-different-domain/2640/6 "2005-02-23T16:25:25Z")

</div>

The HTTP::redirect short-circuits the response and prevents the cookie from being inserted. This affects the explicit cookie insert as well as cookie persistence. However, you can accomplish what you’re trying to do with the HTTP::respond command if you add the Set-Cookie header manually.

---

<div class="post-metadata">

**Author:** ![bl0ndie\_127134](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@bl0ndie\_127134](https://community.f5.com/u/bl0ndie_127134)\
**Post date:** [February 23, 2005, 4:50pm UTC](https://community.f5.com/t/insert-cookie-for-a-different-domain/2640/7 "2005-02-23T16:50:06Z")

</div>

Here is an example …

```tcl
set cookie [format "%s=%s; path=/; domain=%s" $ckname $ckvalue $domain] 
  
 HTTP::respond 301 "Set-Cookie" $cookie

```

The HTTP respond command is more general and lets you specify the http headers or even content to be sent as a response. Hopefully this will do the trick.

---

<div class="post-metadata">

**Author:** ![Aaron\_Magruder](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@Aaron\_Magruder](https://community.f5.com/u/Aaron_Magruder)\
**Post date:** [February 23, 2005, 5:42pm UTC](https://community.f5.com/t/insert-cookie-for-a-different-domain/2640/8 "2005-02-23T17:42:08Z")

</div>

Thanks. I figured out the location field, got the cookie inserted based on your syntax and the redirect is working.

The cookie isn’t being presented to the new site. I am trying to figure out the expires or maxage syntax so that I shows up in my Cookies folder in Windows. I think once this is done, problem solved.

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![Aaron\_Magruder](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@Aaron\_Magruder](https://community.f5.com/u/Aaron_Magruder)\
**Post date:** [February 23, 2005, 6:04pm UTC](https://community.f5.com/t/insert-cookie-for-a-different-domain/2640/9 "2005-02-23T18:04:31Z")

</div>

I had the ckname and ckdomain set incorrectly. After correcting this issue, the redirect works. If the cookie is not presented to aac1, you are redirected back to aac to get a cookie.

Thanks for your help.

when CLIENT\_ACCEPTED {

set ckname acc1

set ckvalue [IP::client\_addr]

set ckdomain [domain.com](http://domain.com)

set cookie [format “%s=%s; path=/; domain=%s” $ckname $ckvalue $ckdomain]

}

when HTTP\_RESPONSE {

HTTP::respond 302 Location “[http://acc1.domain.com](http://acc1.domain.com)” “Set-Cookie” $cookie

}

rule aac1

when CLIENT\_ACCEPTED {

set ckname acc1

set ckvalue [IP::client\_addr]

set ckdomain [domain.com](http://domain.com)

}

when HTTP\_REQUEST {

if {[HTTP::cookie exists $ckname]} {

pool Shared-172-29-98-77

}

else {

HTTP::redirect “[http://acc.domain.com](http://acc.domain.com)”

}

}
