# Cgi.header redirect

**URL:** <https://www.ruby-forum.com/t/cgi-header-redirect/136820>\
**Category:** Ruby\
**Created:** [May 6, 2008, 5:25pm UTC](https://www.ruby-forum.com/t/cgi-header-redirect/136820 "2008-05-06T17:25:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![haris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ccd318/32.png) [@haris](https://www.ruby-forum.com/u/haris)\
**Post date:** [May 6, 2008, 5:25pm UTC](https://www.ruby-forum.com/t/cgi-header-redirect/136820/1 "2008-05-06T17:25:52Z")

</div>

Hi!

I tried to redirect a html page to itself to avoid browser’s question  
about  
resending post data but it doesn’t work.  
I tried this:

cgi=CGI.new  
cgi.header(‘status’=\>‘302 moved’, ‘location’=\>‘my\_page.rb’)  
or  
cgi.header(‘status’=\>‘REDIRECT’, ‘location’=\>‘my\_page.rb’)

The browser still asks the question about post data when I click the  
Refresh  
button

Maybe someone knows why this doesn’t work ?

Thank you.

---

<div class="post-metadata">

**Author:** ![haris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ccd318/32.png) [@haris](https://www.ruby-forum.com/u/haris)\
**Post date:** [May 10, 2008, 7:42pm UTC](https://www.ruby-forum.com/t/cgi-header-redirect/136820/2 "2008-05-10T19:42:41Z")

</div>

“Haris B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> button  
> The browser is doing the right thing. It doesn’t matter that the  
> result of a POST was a redirect (which is very common).

If you click the refresh button after a POST, the browser will want to  
redo the POST. It of course asks if you want to POST the same data,  
which is good (especially if you just posted your credit card number).

There are two ways around this, depending on what you are trying to  
do. If you are just trying to refresh the page by hand without the  
warning box, click in the location bar and press return.

If you want to refresh automatically, without the warning box, add a  
meta refresh tag to the HTML.

---

<div class="post-metadata">

**Author:** ![haris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ccd318/32.png) [@haris](https://www.ruby-forum.com/u/haris)\
**Post date:** [May 11, 2008, 11:21am UTC](https://www.ruby-forum.com/t/cgi-header-redirect/136820/3 "2008-05-11T11:21:51Z")

</div>

On 10 Mai, 19:40, Peter J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > The browser still asks the question about post data when I click the Refresh  
> > do. If you are just trying to refresh the page by hand without the  
> > warning box, click in the location bar and press return.
> 
> If you want to refresh automatically, without the warning box, add a  
> meta refresh tag to the HTML.

According to [HTTP/1.1: Status Code Definitions](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)  
you may want to try a 303 code… But unless request type is HEAD  
(which it is probably not in your case) you should also reply with  
some short HTML text.

Regards,  
Kai

---

<div class="post-metadata">

**Author:** ![haris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ccd318/32.png) [@haris](https://www.ruby-forum.com/u/haris)\
**Post date:** [May 11, 2008, 11:59am UTC](https://www.ruby-forum.com/t/cgi-header-redirect/136820/4 "2008-05-11T11:59:56Z")

</div>

Haris B. wrote:

> I tried this:
> 
> cgi=CGI.new  
> cgi.header(‘status’=\>‘302 moved’, ‘location’=\>‘my\_page.rb’)  
> or  
> cgi.header(‘status’=\>‘REDIRECT’, ‘location’=\>‘my\_page.rb’)

You need both a ‘302’ line and a ‘Location’ header. (btw, according to  
refs the url needs to be absolute.)

> Maybe someone knows why this doesn’t work ?

Why did you try ‘REDIRECT’?

You simply guessed?

Then why didn’t you guess ‘Boozoonga’? or ‘Kalibula’?

It’s nice to invent things, but don’t expect your browser to understand  
you.

Peter J. wrote:

> The browser is doing the right thing.

Yes, but Haris’s is a very valid wish. The Drupal CMS redirects after a  
form post, and I think it’s indeed a good thing to do.
