# Storing URI in a cookie

**URL:** <https://www.ruby-forum.com/t/storing-uri-in-a-cookie/211404>\
**Category:** NGINX\
**Created:** [September 2, 2011, 10:42pm UTC](https://www.ruby-forum.com/t/storing-uri-in-a-cookie/211404 "2011-09-02T22:42:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [September 2, 2011, 10:42pm UTC](https://www.ruby-forum.com/t/storing-uri-in-a-cookie/211404/1 "2011-09-02T22:42:57Z")

</div>

I dont know why I am struggling with this so much but I need a bit of  
help.  
I am trying to redirect users to a verification page and then back to  
their  
original requested URI but because of the rewrite I lose both the $uri  
and  
$request\_uri variables. I figure the easiest way to solve this problem  
is  
to store the original URI in a cookie and read it from that after the  
verification page. I cant seem to store the cookie correctly and read  
it  
back. Can anyone provide some direction? Also, is there any risk that  
by  
doing this I disrupt other cookies being stored by my application?

Thanks!

Rami

---

<div class="post-metadata">

**Author:** ![Rami\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f05b48/32.png) [@Rami\_E](https://www.ruby-forum.com/u/Rami_E)\
**Post date:** [September 3, 2011, 10:50am UTC](https://www.ruby-forum.com/t/storing-uri-in-a-cookie/211404/2 "2011-09-03T10:50:48Z")

</div>

may try this:  
add\_header Set-Cookie “uri=$uri; path=/”;

Posted at Nginx Forum:

> **[Re: Storing URI in a cookie](https://forum.nginx.org/read.php?2,214665,214675#msg-214675)**
>
> I dont know why I am struggling with this so much but I need a bit of help. I am trying to redirect users to a verification page and then back to their original requested URI but because of the rewrite I lose both the $uri and $request\_uri variables....

---

<div class="post-metadata">

**Author:** ![Rami\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f05b48/32.png) [@Rami\_E](https://www.ruby-forum.com/u/Rami_E)\
**Post date:** [September 3, 2011, 11:05am UTC](https://www.ruby-forum.com/t/storing-uri-in-a-cookie/211404/3 "2011-09-03T11:05:39Z")

</div>

A better way to redirect users is to store the uri in the redirection  
url.  
Storing it in a cookie causes problems if user opens two different tabs  
with  
two different uris for example.  
On Sep 2, 2011 11:42 PM, “Rami E.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I dont know why I am struggling with this so much but I need a bit of  
> help.  
> I am trying to redirect users to a verification page and then back to  
> their

---

<div class="post-metadata">

**Author:** ![Rami\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f05b48/32.png) [@Rami\_E](https://www.ruby-forum.com/u/Rami_E)\
**Post date:** [September 3, 2011, 4:43pm UTC](https://www.ruby-forum.com/t/storing-uri-in-a-cookie/211404/4 "2011-09-03T16:43:37Z")

</div>

Thanks guys for the suggestions.

@calin - I have tried doing that but it messes with the verification  
page  
and am not sure how to implement it correctly.

@Magicbear - I have put that code in there before the rewrite but then  
after  
the rewrite when I try to access that cookie it is not there. I should  
have  
been a little more clear on what I have tried and my config. Here is  
the  
flow of the rewirte.

location / {  
error\_page 503 = @recaptcha;  
if ($verifyvariable)  
{  
return 503;  
}  
}

```
    location @recaptcha {

        add_header Set-Cookie "originrequest=$request_uri path=/

```

expires=2m";  
secure\_cookie  
$cookie\_CAPTCHA\_SESSION,$cookie\_CAPTCHA\_EXPIRES;  
rewrite ^ /captcha.html redirect;  
}

location = /captcha.html {  
#this has a form that submits information and goes to  
/verify  
}

location = /verify {  
eval\_inherit\_body on;  
eval\_override\_content\_type ‘text/plain’;

eval $verify\_content {  
proxy\_method POST;  
proxy\_set\_header Accept-Encoding “”;  
proxy\_set\_body ;  
rewrite .\* /recaptcha/api/verify break;  
proxy\_pass ‘[http://www.google.com](http://www.google.com)’;  
}

if ($verify\_content ~\* ^true[\s\R]_(._)) {  
set $error\_code $1;  
rewrite .\* /set\_secure\_cookie last;  
}

}

location = /set\_secure\_cookie {  
internal;

## SETTING

rewrite ^ /$cookie\_ originrequest redirect;  
}
