简体   繁体   中英

rack-rewrite code to redirect https to http (on heroku)

Somehow Google indexed my homepage as https://mydomain.com . When you do a site:mydomain.com search, the first result is https://mydomain.com and I don't have a SSL cert and don't want to do https. Now our visitors get the ugly warnings in their browsers, of course (because heroku serves their *.heroku cert by default).

It seems that I can do a 301 redirect with the rack-rewrite gem but I just can't find how.

So, what is a rack-rewrite recipe to redirect all https:// to http:// ? All I can find is information on how to do the reverse thing or to make the canonical redirections.

Hmm, untested, but would something like this work?

r301 %r{.*}, 'http://non-secure-domain.com$&', :if => Proc.new {|rack_env|
  rack_env['SERVER_PORT'] != '80'
}

The documentation of rack-rewrite mentions a nice way at https://github.com/jtrupiano/rack-rewrite#scheme

# Redirect all https traffic to http
r301 %r{.*}, 'http://www.example.tld$&', :scheme => 'https'

Using the scheme option from rack-rewrite will only cause an infinite loop on heroku. You also can't on the port being 80 because of the way heroku will proxy to your workers. Because of this and way the routing layer works, you have to check the HTTP_X_FORWARDED_PROTO header:

r301 %r{.*}, 'http://example.com$&', :if => Proc.new { |rack_env|
  rack_env['HTTP_X_FORWARDED_PROTO'] == 'https'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM