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

前端 未结 6 708
感动是毒
感动是毒 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:37

    Rails 3.1.0 and higher has force_ssl, which is a controller method that will redirect to https for non-development environments.

    http://api.rubyonrails.org/classes/ActionController/ForceSSL/ClassMethods.html

    Place it in each controller that you want to redirect, or better yet, place it in your ApplicationController:

    app/controllers/application.rb:

    class ApplicationController < ActionController::Base
      # ...
      force_ssl
      # ...
    end
    

    This is a good thing to always include in your apps (and of course you'll have to get a certificate). HTTPS Everywhere!

提交回复
热议问题