Ruby-on-rails Rails - 如何从 http://example.com 重定向到 https://www.example.com

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4329176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:46:08  来源:igfitidea点击:

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

ruby-on-railsruby-on-rails-3dnsheroku

提问by AnApprentice

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

我正在学习如何清理我的应用程序的 URL。我的应用程序由 Heroku 上的 Rails 3 提供支持。

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

所需的网址是 https://www.example.comite.com

I'd like to redirect all URLs unlike the above to that URL. Is this a Rails thing or DNS?

我想将所有与上述不同的 URL 重定向到该 URL。这是 Rails 的东西还是 DNS?

Bad URLs:

错误的网址:

https://example.comite.com
http://www.example.comite.com
http://example.comite.com

And if anything is trailing, like http://www.example.comite.com/photo/1for the url to be redirected with the path: https://www.example.comite.com/photo/1

如果有任何结尾,例如http://www.example.comite.com/photo/1要使用路径重定向的 url:https://www.example.comite.com/photo/1

采纳答案by edgerunner

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.

DNS 记录无法定义域的协议,因此您无法通过 DNS重定向http://https://。通过 Web 服务器配置来完成它是不可移植的、难以做到的、容易出错的并且已经过时了。这是最好由 Rails 路由器处理的工作。

# 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 => "" }

回答by Jon

As an extension to user2100689's answer, in Rails 3+ you can use config.force_ssl = truein config/environments/production.rb

作为user2100689 答案的扩展,在 Rails 3+ 中,您可以config.force_ssl = trueconfig/environments/production.rb 中使用

The line can just be uncommented as follows

该行可以取消注释如下

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
config.force_ssl = true

回答by user693960

Because this is Heroku, you cannot use apache or nginx configs. What you can do is put a before_filter in your ApplicationController, assuming you have 3 or more controllers like these below, although of course they will be in separate files

因为这是 Heroku,所以不能使用 apache 或 nginx 配置。你可以做的是在你的 ApplicationController 中放置一个 before_filter,假设你有 3 个或更多像下面这样的控制器,当然它们将在单独的文件中

class ApplicationController < ActionController::Base
    def redirect_https        
        redirect_to :protocol => "https://" unless request.ssl?
        return true
    end
    before_filter :redirect_https
end
class TypicalController < ApplicationController
    def blah
    end
end
class HomePageController < ApplicationController
    skip_before_filter :redirect_https
end

You may also need to fiddle your routes a bit when using devise, but I suspect that was just the way we did it so I won't get into those details here, and I've modified the code above to avoid that complication.

在使用 devise 时,您可能还需要稍微调整您的路线,但我怀疑这正是我们这样做的方式,所以我不会在这里详细介绍这些细节,并且我已经修改了上面的代码以避免这种复杂化。

happy hacking.

快乐的黑客。

回答by Robin Daugherty

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

Rails 3.1.0 及更高版本具有force_ssl,这是一个控制器方法,将重定向到非开发环境的 https。

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

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:

把它放在你想要重定向的每个控制器中,或者更好的是,把它放在你的 ApplicationController 中:

app/controllers/application.rb:

应用程序/控制器/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!

始终将其包含在您的应用程序中是一件好事(当然,您必须获得证书)。HTTPS无处不在!

回答by user2100689

You can always throw this in your production.rb... config.use_ssl = true

你总是可以把它扔到你的 production.rb 中... config.use_ssl = true

回答by Flukey

DO it in your vhosts file.

在您的 vhosts 文件中执行此操作。

Setup a SSL vhost.

设置 SSL 虚拟主机。

In your standard port 80 virtual host. Add this to the config:

在您的标准端口 80 虚拟主机中。将此添加到配置中:

Redirect permanent / https://www.mysite.com

This will forward all port 80 requests to https.

这会将所有端口 80 请求转发到 https。