我有这个第一个中间件:
class RedirectIt
require "net/https"
require "uri"
require 'open-uri'
APP_DOMAIN = 'http://www.konkurrencerher.dk'
def initialize(app)
@app = app
end
#
def call(env)
request = Rack::Request.new(env)
if request.env['HTTP_HOST'] != APP_DOMAIN
#REDIRECT TIL WWW
[301, { "Location" => "#{APP_DOMAIN}#{request.fullpath()}"}, ["Redirecting..."]]
elsif "#{request.fullpath()}".split(".").last == "png"
#PROXY BILLEDER
status, headers, response = @app.call(env)
headers['Cache-Control'] = "public, max-age=#{84.hours.to_i}"
headers['Content-Type'] = 'image/png'
headers['Content-Disposition'] = 'inline'
response_body = "#{(open('http://s3-eu-west-1.amazonaws.com/konkurrencerher#{request.path()}')).read}"
[status, headers, response_body]
else
@app.call(env)
end
end
end
答案 0 :(得分:2)
HTTP_HOST
只是裸主机名,没有协议(即www.konkurrencerher.dk
),因此永远不会等于您的APP_DOMAIN
常量,并且您将始终重定向。