# How to redirect 302 in code?

**URL:** <https://www.ruby-forum.com/t/how-to-redirect-302-in-code/174358>\
**Category:** NGINX\
**Created:** [August 25, 2009, 3:13pm UTC](https://www.ruby-forum.com/t/how-to-redirect-302-in-code/174358 "2009-08-25T15:13:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wangbin579](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bc79bd/32.png) [@wangbin579](https://www.ruby-forum.com/u/wangbin579)\
**Post date:** [August 25, 2009, 3:13pm UTC](https://www.ruby-forum.com/t/how-to-redirect-302-in-code/174358/1 "2009-08-25T15:13:28Z")

</div>

the following code does not work in nginx-0.8.6,who can tell me why? thx

location = ngx\_palloc(r-\>pool, strlen(url));  
…  
r-\>headers\_out.location = ngx\_palloc(r-\>pool,  
sizeof(ngx\_table\_elt\_t));  
…  
ngx\_copy(location, url, strlen(url));  
r-\>headers\_out.location-\>value.data = location;  
r-\>headers\_out.location-\>value.len = strlen(url);  
r-\>headers\_out.content\_length\_n = 0;  
r-\>header\_only = 1;  
r-\>keepalive = 0;  
return NGX\_HTTP\_MOVED\_TEMPORARILY ;

Posted at Nginx Forum:

> **[how to redirect 302 in code?](https://forum.nginx.org/read.php?2,5158,5158#msg-5158)**
>
> the following code does not work as i expected in nginx-0.8.6,who can tell me why? thx location = ngx\_palloc(r-\>pool, strlen(url)); ... r-\>headers\_out.location = ngx\_palloc(r-\>pool, sizeof(ngx\_table\_elt\_t)); ... ngx\_copy(location, u

---

<div class="post-metadata">

**Author:** ![wangbin579](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bc79bd/32.png) [@wangbin579](https://www.ruby-forum.com/u/wangbin579)\
**Post date:** [August 25, 2009, 4:20pm UTC](https://www.ruby-forum.com/t/how-to-redirect-302-in-code/174358/2 "2009-08-25T16:20:48Z")

</div>

At Tue, 25 Aug 2009 09:04:09 -0400,  
“wangbin579” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Ok. Follow code work:

header = ngx\_list\_push(&r-\>headers\_out.headers);  
if (header == NULL) {  
return NGX\_HTTP\_INTERNAL\_SERVER\_ERROR;  
}

header-\>hash = 1;  
header-\>key.len = sizeof(“Location”) - 1;  
header-\>key.data = (u\_char \*) “Location”;  
header-\>value.len = sizeof(“[http://google.com/](http://google.com/)”) - 1;  
header-\>value.data = (u\_char \*) “[http://google.com/](http://google.com/)” - 1;

return NGX\_HTTP\_MOVED\_TEMPORARILY;

---

<div class="post-metadata">

**Author:** ![wangbin579](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bc79bd/32.png) [@wangbin579](https://www.ruby-forum.com/u/wangbin579)\
**Post date:** [August 26, 2009, 9:03pm UTC](https://www.ruby-forum.com/t/how-to-redirect-302-in-code/174358/3 "2009-08-26T21:03:04Z")

</div>

wow,it works  
thanks

Posted at Nginx Forum:

> **[Re: how to redirect 302 in code?](https://forum.nginx.org/read.php?2,5158,5174#msg-5174)**
>
> the following code does not work as i expected in nginx-0.8.6,who can tell me why? thx location = ngx\_palloc(r-\>pool, strlen(url)); ... r-\>headers\_out.location = ngx\_palloc(r-\>pool, sizeof(ngx\_table\_elt\_t)); ... ngx\_copy(location, u
