如何使用 Rack 执行 HTTP 重定向(302)?

How to perform an HTTP redirect (302) using Rack?

我正在弄乱我的第一个 Rack 应用程序(这只是为了实验)。

当有电话打进来时,我会这样做:

class Application
    def call(env)
      # Totally ignore favicons for the time being
      if env['PATH_INFO'] == '/favicon.ico'
        return [ 404, {'Content-Type' => 'text/html'}, [] ]
      elsif env['PATH_INFO'] == '/'
        return [ 302, {'http-equiv' => "refresh", 'content' => "2;url=http://google.com"}, [] ]
      end
      ...

我知道这很糟糕...但是,这不是一个严肃的项目。

我正在尝试弄清楚如何进行重定向。我所拥有的不起作用。基本上,当您在我的网站上点击 / 时,我想将要求重定向到 google.com.

这是重定向到 Google

的工作应用
require 'rack'
require 'rack/server'

class HelloWorld
  def response
    [ 302, {'Location' =>"http://google.com"}, [] ]
  end
end

class HelloWorldApp
  def self.call(env)
    HelloWorld.new.response
  end
end

Rack::Server.start :app => HelloWorldApp