# Multi-Domain Cookie Issue

**URL:** <https://community.f5.com/t/multi-domain-cookie-issue/57872>\
**Category:** Technical Forums\
**Tags:** dev, irules, devops, application-delivery\
**Created:** [June 26, 2010, 8:02pm UTC](https://community.f5.com/t/multi-domain-cookie-issue/57872 "2010-06-26T20:02:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Miller](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/chris_miller/32/14702_2.png) [@Chris\_Miller](https://community.f5.com/u/Chris_Miller)\
**Post date:** [June 26, 2010, 8:02pm UTC](https://community.f5.com/t/multi-domain-cookie-issue/57872/1 "2010-06-26T20:02:31Z")

</div>

Here’s my situation…

I have a site, [www.sample.com](http://www.sample.com) - configured with Akamai. The “origin” points to a VS on my LTM to which I have an iRule that sends users to different pools depending on a cookie value. I want to allow certain users to hit a specific pool but also want them to do so through Akamai. Since access to Akamai is wide open, I need them to hit my “origin” first and if they match my whitelist, I’ll set a cookie and redirect them.

My initial assumption was that I’d do something like this:

1. A user hits [origin.sample.com/1](http://origin.sample.com/1)

2. My iRule inserts a cookie named “x” with the value of whatever is behind the / - in this case, a 1.

3. The iRule redirects them to [www.sample.com](http://www.sample.com) (which goes through Akamai) and since they have a cookie value of 1, they’d go to the right pool.

Unfortunately, I forgot that the cookie “x” with value of “1” would be for the domain “[origin.sample.com](http://origin.sample.com)” and not “[www.sample.com](http://www.sample.com)” so this likely wouldn’t work.

Can anyone think of a better way to do this? Could I configure an iRule on my origin VS that looks at cookies from a specific domain?

---

<div class="post-metadata">

**Author:** ![hoolio](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hoolio](https://community.f5.com/u/hoolio)\
**Post date:** [June 26, 2010, 9:15pm UTC](https://community.f5.com/t/multi-domain-cookie-issue/57872/2 "2010-06-26T21:15:39Z")

</div>

Hi Chris,

Couldn’t you set the domain on the x cookie to [sample.com](http://sample.com) so that clients will send the cookie on all requests to any [sample.com](http://sample.com) domain?

Aaron

---

<div class="post-metadata">

**Author:** ![Chris\_Miller](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/chris_miller/32/14702_2.png) [@Chris\_Miller](https://community.f5.com/u/Chris_Miller)\
**Post date:** [June 28, 2010, 1:13am UTC](https://community.f5.com/t/multi-domain-cookie-issue/57872/3 "2010-06-28T01:13:14Z")

</div>

Posted By hoolio on 06/26/2010 02:15 PM

Hi Chris,

Couldn’t you set the domain on the x cookie to [sample.com](http://sample.com) so that clients will send the cookie on all requests to any [sample.com](http://sample.com) domain?

Aaron

Aaron - I love having you around! I should have noticed the “domain” option in the http::cookie wiki. Thanks!

---

<div class="post-metadata">

**Author:** ![Chris\_Miller](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/chris_miller/32/14702_2.png) [@Chris\_Miller](https://community.f5.com/u/Chris_Miller)\
**Post date:** [June 28, 2010, 2:28pm UTC](https://community.f5.com/t/multi-domain-cookie-issue/57872/4 "2010-06-28T14:28:54Z")

</div>

Aaron - how would I go about checking to see whether a cookie exists for a different domain? If they have a cookie for [www.example.com](http://www.example.com), I’ll need to remove it before inserting my new one…

---

<div class="post-metadata">

**Author:** ![hoolio](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hoolio](https://community.f5.com/u/hoolio)\
**Post date:** [June 28, 2010, 3:22pm UTC](https://community.f5.com/t/multi-domain-cookie-issue/57872/5 "2010-06-28T15:22:51Z")

</div>

You could check the domain on each cookie using a loop of HTTP::cookie domain against a list from HTTP::cookie names. Are you concerned that you might already have a cookie x set by the app?

Aaron

---

<div class="post-metadata">

**Author:** ![Chris\_Miller](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/chris_miller/32/14702_2.png) [@Chris\_Miller](https://community.f5.com/u/Chris_Miller)\
**Post date:** [June 28, 2010, 3:36pm UTC](https://community.f5.com/t/multi-domain-cookie-issue/57872/6 "2010-06-28T15:36:55Z")

</div>

Posted By hoolio on 06/28/2010 08:22 AM

You could check the domain on each cookie using a loop of HTTP::cookie domain against a list from HTTP::cookie names. Are you concerned that you might already have a cookie x set by the app?

Aaron

If a user first goes to [www.sample.com](http://www.sample.com), they’ll get sent to pool 1 or pool 2 - if pool 1, they’ll get a cookie set to “1”. Now, if they specify [origin.sample.com/2](http://origin.sample.com/2), I need to check whether a cookie exists for [www.sample.com](http://www.sample.com), rewrite its value to “2”, and redirect them to [www.sample.com](http://www.sample.com).

---

<div class="post-metadata">

**Author:** ![hoolio](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hoolio](https://community.f5.com/u/hoolio)\
**Post date:** [June 28, 2010, 3:57pm UTC](https://community.f5.com/t/multi-domain-cookie-issue/57872/7 "2010-06-28T15:57:24Z")

</div>

The HTTP request doesn’t include any details for cookies other than the cookie name and value. So you couldn’t tell just from the request what domain a cookie was set for. You might be able to set the name or value with a unique string that tells you what VS (or domain) the cookie was set for.

Aaron

---

<div class="post-metadata">

**Author:** ![Chris\_Miller](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/chris_miller/32/14702_2.png) [@Chris\_Miller](https://community.f5.com/u/Chris_Miller)\
**Post date:** [June 28, 2010, 4:32pm UTC](https://community.f5.com/t/multi-domain-cookie-issue/57872/8 "2010-06-28T16:32:05Z")

</div>

Posted By hoolio on 06/28/2010 08:57 AM

The HTTP request doesn’t include any details for cookies other than the cookie name and value. So you couldn’t tell just from the request what domain a cookie was set for. You might be able to set the name or value with a unique string that tells you what VS (or domain) the cookie was set for.

Aaron

If I try to set a cookie for domain [www.sample.com](http://www.sample.com) that already exists - what would the browser do?

---

<div class="post-metadata">

**Author:** ![hoolio](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hoolio](https://community.f5.com/u/hoolio)\
**Post date:** [June 28, 2010, 4:48pm UTC](https://community.f5.com/t/multi-domain-cookie-issue/57872/9 "2010-06-28T16:48:10Z")

</div>

If the domain on the cookie (and all other attributes), the browser should replace the old cookie with the new cookie value. If the domain or other attribute (like path) is different, then the client will store two cookies. The simplest way to check the exact behavior would be to use a browser plugin like HttpFox and test it.

Aaron

---

<div class="post-metadata">

**Author:** ![Chris\_Miller](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/chris_miller/32/14702_2.png) [@Chris\_Miller](https://community.f5.com/u/Chris_Miller)\
**Post date:** [June 28, 2010, 7:15pm UTC](https://community.f5.com/t/multi-domain-cookie-issue/57872/10 "2010-06-28T19:15:02Z")

</div>

Posted By hoolio on 06/28/2010 09:48 AM

If the domain on the cookie (and all other attributes), the browser should replace the old cookie with the new cookie value. If the domain or other attribute (like path) is different, then the client will store two cookies. The simplest way to check the exact behavior would be to use a browser plugin like HttpFox and test it.

Aaron

For whatever reason - the domain cookie insert doesn’t appear to be working. I’m basically saying : If user hits [x.sample.com/1](http://x.sample.com/1), set cookie “x” with value “1” domain “[www.awesome.com](http://www.awesome.com)” http::redirect “[http://www.awesome.com](http://www.awesome.com)” This doesn’t appear to be setting a cookie for the [www.awesome.com](http://www.awesome.com) domain - any ideas?

---

<div class="post-metadata">

**Author:** ![hoolio](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hoolio](https://community.f5.com/u/hoolio)\
**Post date:** [June 28, 2010, 7:50pm UTC](https://community.f5.com/t/multi-domain-cookie-issue/57872/11 "2010-06-28T19:50:04Z")

</div>

HTTP user agents enforce a domain “firewall” to prevent one site from setting, accessing or modifying cookies from another domain. A subdomain can set cookies for it’s own root domain though. So [site1.example.com](http://site1.example.com) can set cookies for [example.com](http://example.com) which will be sent by clients to [site2.example.com](http://site2.example.com). But as you found, no subdomain on [example.com](http://example.com) can’t set cookies for [example2.com](http://example2.com) or any other domain.

See this wikipedia article for details:

[http://en.wikipedia.org/wiki/Same\_origin\_policy](http://en.wikipedia.org/wiki/Same%5C_origin%5C_policy)

Can you use subdomains for this? If not, I think you’ll need to re-architect the solution. I suppose you could insert something in the URI in the redirect which contains similar info.

Aaron

---

<div class="post-metadata">

**Author:** ![Chris\_Miller](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/chris_miller/32/14702_2.png) [@Chris\_Miller](https://community.f5.com/u/Chris_Miller)\
**Post date:** [July 3, 2010, 2:02pm UTC](https://community.f5.com/t/multi-domain-cookie-issue/57872/12 "2010-07-03T14:02:47Z")

</div>

Hoolio - thanks a ton for all the help with this. I got it working using subdomains, specifying the cookie domain as .example.com, using HTTP::respond instead of HTTP::redirect, and by using Set-Cookie rather than HTTP::cookie insert.
