I have this middleware which is the first:
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
My Heroku log: http://pastie.org/private/wfhvsr2lqfmq9zknenx7q
The
HTTP_HOSTis just the bare hostname, without the protocol (i.e.www.konkurrencerher.dk), so will never be equal to yourAPP_DOMAINconstant and you’ll always redirect.