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

发布于 2024-10-05 15:59:29 字数 445 浏览 3 评论 0原文

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

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

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

错误的 URL:

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

如果有任何内容尾随,例如 http://www.example.comite.com/photo/1 ,则要使用以下路径重定向 URL:https://www .example.comite.com/photo/1

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'd like to redirect all URLs unlike the above to that URL. Is this a Rails thing or 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/1 for the url to be redirected with the path: https://www.example.comite.com/photo/1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

随梦而飞# 2024-10-12 15:59:30

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

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

地点它位于您想要重定向的每个控制器中,或者更好的是,将其放置在您的 ApplicationController 中:

app/controllers/application.rb:

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

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

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!

短叹 2024-10-12 15:59:30

您始终可以将其放入 Production.rb...config.use_ssl = true

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

盛装女皇 2024-10-12 15:59:30

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

设置 SSL 虚拟主机。

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

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

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

DO it in your vhosts file.

Setup a SSL vhost.

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

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

This will forward all port 80 requests to https.

悍妇囚夫 2024-10-12 15:59:29

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

该行可以取消注释,如下所示

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

As an extension to user2100689's answer, in Rails 3+ you can use config.force_ssl = true in config/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
花伊自在美 2024-10-12 15:59:29

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

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 => "" }
决绝 2024-10-12 15:59:29

因为这是 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

在使用设计时您可能还需要稍微调整一下路线,但我怀疑这是
就像我们所做的那样,所以我不会在这里讨论这些细节,并且我已经修改了代码
上面以避免这种并发症。

快乐的黑客。

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

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.

happy hacking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文