Welcome! Log In Create A New Profile

Advanced

Cookie redirect from homepage

Posted by gaspy 
Cookie redirect from homepage
June 28, 2016 04:22AM
Hi,

I'm trying to redirect to a different page from homepage based on a cookie. Secondary objective is to also set that cookie. The method works with Javascript but when I try to do if with nginx, it doesn't work consistently.

I have several location rules, but the one that's matched for homepage is
location /
{
try_files $uri $uri/ /cms/assets/cache/supercache/$http_host/$cache_uri/$file /cms/index.php?$args;
gzip_static on;
expires 1d;

add_header X-Test default;
}

The $cache_uri and $file vars are set elsewhere and they work.

Now, for homepage I added this rule:
location = /index.html
{
gzip_static on;
expires 1d;

add_header X-Test root;

if ($cookie_prefer ~* "someval")
{
return 302 /somepage.html?$args;
}
}
Somehow this doesn't work reliably. Crazy as it sounds, it works... sometimes. I suspect a caching issue or something, but it's even more frustrating than if it wasn't working at all.

Am I doing something wrong? Is there a better, fool-proof way for cookie redirect?

Second question is about setting the cookie itself. I know I can add a cookie with add_header but how can I specify the expiration as 1 month from now?
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 110
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready