I want to redirect (rewrite with a 200) to different locations based on the presence of a cookie. This is possible according to the docs. However, because you are redirecting based on the presence of a cookie and not the cookie’s value, I have to create two different cookies and redirect accordingly:
Example netlify.toml
file:
[[redirects]]
from = "/*"
to = "/dir-1/:splat"
status = 200
force = true
conditions = { Cookie = ["dir-1"] }
[[redirects]]
from = "/*"
to = "/dir-2/:splat"
status = 200
force = true
conditions = { Cookie = ["dir-2"] }
The tricky part here is that I have to also unset cookies, because an incoming request could be have an old cookie on it, but from what I can tell, there’s no way to set two different set-cookie headers.
I’m not sure how to get around this issue. Redirecting based on a cookie’s value would make it so I could set/unset a single cookie with a different value each time, but AFAIK there’s no way to do this with netlify’s redirect rules.
Does this require something like using an edge function?
What I’m trying to do:
I want to be able to have netlify’s routing work as normal unless a particular cookie is present, in which case I want to be able to rewrite requests to a matching subdirectory based on the presence/value of that cookie.
Example project structure:
.
├── index.html
├── dir-1/
│ └── index.html
├── dir-2/
│ └── index.html
For all normal incoming requests to /
, they should get served index.html
.
A request to /
with a cookie, such as dir=1
, will get served dir-1/index.html
And a request to /
with a cookie, such as dir=2
, will get served dir-2/index.html
And I would need an endpoint that allows setting these, i.e. /cookies?dir=1
would set that cookie and /cookies?dir=2
would set that cookie.
This obviously requires stitching together the ability to do redirect/rewrites as well as setting cookies and I’m having trouble finding a way to do it with existing tools from netlify (short of trying the new deno edge functions, which presumably could do this?)