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

Rob*_*eph 0 ruby rack http

我正在弄乱我的第一个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
      ...
Run Code Online (Sandbox Code Playgroud)

我知道这太可怕了……但是,这并不是一个认真的项目。

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

Wan*_*ker 5

这是可以重定向到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
Run Code Online (Sandbox Code Playgroud)