redirect to 'www' before force_ssl

前端 未结 3 1113
孤独总比滥情好
孤独总比滥情好 2021-02-10 19:14

I\'m moving an app to heroku and am having some issues with ssl and redirects.

I\'m on rails 3.1 and I\'ve tried forcing ssl with middleware in the environments producti

3条回答
  •  醉梦人生
    2021-02-10 19:52

    Here is how I solved the problem. I removed config.force_ssl = true from production.rb and instead used:

    Add this method to ApplicationController

      def force_ssl
        if Rails.env.production?
          redirect_to :protocol => 'https' unless request.ssl?
        end
      end  
    

    And add it as a before filter on ApplicationController

    before_filter :force_ssl
    

    I am also using a ensure_domain which switches from http://example.com to http://www.example.com. Make sure such a before filter is called before force_ssl.

提交回复
热议问题