Rails - How to Redirect from http://example.com to https://www.example.com

前端 未结 6 694
感动是毒
感动是毒 2020-12-23 16:56

I\'m looking to learn how to cleanup my app\'s URLs. My app is powered by Rails 3 on Heroku.

The desired URL is https://www.example.comite.com

I

6条回答
  •  不思量自难忘°
    2020-12-23 17:38

    DNS records cannot define the protocol for a domain, therefore you can't redirect http:// to https:// through DNS. Doing it through the web server configuration is not portable, hard to do, error prone and just plain outdated. This is a job best handled by the Rails router.

    # beginning of routes.rb 
    match "*path" => redirect("https://www.mysite.com/%{path}"), :constraints => { :protocol => "http://" }
    match "*path" => redirect("https://www.mysite.com/%{path}"), :constraints => { :subdomain => "" }
    

提交回复
热议问题