Ruby HTTP redirect?

Hi,

I’m new to Ruby, only used Perl and PHP before.

I know there is no method “header” (as in PHP) or “redirect” in Ruby’s CGI
class, but I need to know how to do HTTP redirects in Ruby.

Any suggestions?

Thanks in advance!

Hi,

I’m new to Ruby, only used Perl and PHP before.

Welcome!

I know there is no method “header” (as in PHP) or “redirect” in Ruby’s CGI
class, but I need to know how to do HTTP redirects in Ruby.

Any suggestions?

The raw text way:

def redirect( new_page )
print "HTTP/1.1 302 OK\r\n"
print "Location:#{new_page}\n\n"
end

The CGI way (off the top of my head)

cgi = CGI.new(“html4”)
cgi.header(“Location” => “/some/other/page”)

James

···

Thanks in advance!

Yes, I tried the first way before,
but I always get a 500 Server Error on my Apache (1.3.26) when doing it.
Same goes for the other way.

What could that mean? Do I need to change the HTTP commands?

I don’t have problems with PHP and Perl redirects using their proper
functions, but as I really like Ruby I want to do it in that language :slight_smile:

Yes, I tried the first way before,
but I always get a 500 Server Error on my Apache (1.3.26) when doing it.
Same goes for the other way.

Um, then try omitting the first header:

def redirect( new_page )
print "Location:#{new_page}\n\n"
end

Sorry, this is also off the top of my head. I know I’ve run into this
myself, but I don;t recal exactly what the details were, and how running
mod_ruby or CGI makes a difference (if any).

BTW, are you using mod_ruby, or is this strictly CGI?

James

Thanks man! :slight_smile:
I omitted the first header as you said and now it works.

I don’t use mod_ruby atm, I’m just messing around with a local
apache here so normal CGI is just fine :slight_smile:

···

Um, then try omitting the first header:

def redirect( new_page )
print "Location:#{new_page}\n\n"
end

Sorry, this is also off the top of my head. I know I’ve run into this
myself, but I don;t recal exactly what the details were, and how running
mod_ruby or CGI makes a difference (if any).

BTW, are you using mod_ruby, or is this strictly CGI?

James