# How to make a redirect via programming?

**URL:** <https://www.ruby-forum.com/t/how-to-make-a-redirect-via-programming/207448>\
**Category:** NGINX\
**Created:** [May 26, 2011, 12:42am UTC](https://www.ruby-forum.com/t/how-to-make-a-redirect-via-programming/207448 "2011-05-26T00:42:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [May 26, 2011, 12:42am UTC](https://www.ruby-forum.com/t/how-to-make-a-redirect-via-programming/207448/1 "2011-05-26T00:42:21Z")

</div>

Hey, I’m making a custom module and need to send back the redirect  
response to the client. It should behave like the “sendRedirect” in java  
servlet programming. So which function is that API?

Thanks.

Posted at Nginx Forum:

> **[How to make a redirect via programming?](https://forum.nginx.org/read.php?2,200929,200929#msg-200929)**
>
> Hey, I'm making a custom module and need to send back the redirect response to the client. It should behave like the "sendRedirect" in java servlet programming. So which function is that API? Thanks.

---

<div class="post-metadata">

**Author:** ![speedfirst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19adc/32.png) [@speedfirst](https://www.ruby-forum.com/u/speedfirst)\
**Post date:** [May 26, 2011, 1:15am UTC](https://www.ruby-forum.com/t/how-to-make-a-redirect-via-programming/207448/2 "2011-05-26T01:15:49Z")

</div>

is this OK?

r-\>headers\_out.status = NGX\_HTTP\_REDIRECT;  
r-\>headers\_out.content\_length\_n = 0;  
… // the code set “Location” header

ngx\_http\_send\_header(r);

Posted at Nginx Forum:

> **[Re: How to make a redirect via programming?](https://forum.nginx.org/read.php?2,200929,200937#msg-200937)**
>
> Hey, I'm making a custom module and need to send back the redirect response to the client. It should behave like the "sendRedirect" in java servlet programming. So which function is that API? Thanks.

---

<div class="post-metadata">

**Author:** ![speedfirst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e19adc/32.png) [@speedfirst](https://www.ruby-forum.com/u/speedfirst)\
**Post date:** [May 26, 2011, 10:03am UTC](https://www.ruby-forum.com/t/how-to-make-a-redirect-via-programming/207448/3 "2011-05-26T10:03:47Z")

</div>

Hello!

On Wed, May 25, 2011 at 06:41:46PM -0400, speedfirst wrote:

> Hey, I’m making a custom module and need to send back the redirect  
> response to the client. It should behave like the “sendRedirect” in java  
> servlet programming. So which function is that API?
> 
> Thanks.

```
r->headers_out.location = ngx_palloc(r->pool, 

```

sizeof(ngx\_table\_elt\_t));  
if (r-\>headers\_out.location == NULL) {  
return NGX\_HTTP\_INTERNAL\_SERVER\_ERROR;  
}

```
r->headers_out.location->value.len = sizeof("http://example.com/") - 

```

1;  
r-\>headers\_out.location-\>value.data = “[http://example.com/](http://example.com/)”;

```
return NGX_HTTP_MOVED_PERMANENTLY;

```

Maxim D.
