Rails重定向与https [英] Rails redirect with https

查看:160
本文介绍了Rails重定向与https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在维护一个Ruby on Rails网站,我对如何使用https协议重定向到相对URL感到困惑。

I'm maintaining a Ruby on Rails site and I'm confused as to how to perform redirects to relative URLs using the https protocol.

我可以成功创建使用http重定向到相对URL,例如:

I can successfully create a redirect to a relative URL using http, for example:

redirect_to "/some_directory/"

但我无法辨别如何使用https协议创建重定向到URL。我只能通过使用绝对URL来实现,例如:

But I cannot discern how to create a redirect to a URL using the https protocol. I have only been able to do so by using absolute URLS, for example:

redirect_to "https://mysite.com/some_directory/"

我想保持我的代码干净,使用相对URL似乎是个好主意。有谁知道如何在Rails中实现这一点?

I would like to keep my code clean, and using relative URLs seems like a good idea. Does anyone know how to achieve this in Rails?

推荐答案

你可能最好使用 ssl_requirement 并且如果链接或重定向是否使用https,则不关心。使用ssl_requirement,您可以声明哪些操作需要SSL,哪些操作需要SSL以及哪些操作不需要使用SSL。

You're probably better off using ssl_requirement and not caring if a link or redirect is or isn't using https. With ssl_requirement, you declare which actions require SSL, which ones are capable of SSL and which ones are required not to use SSL.

如果您要重定向到某个地方以外的地方Rails应用程序,然后将协议指定为Olly建议将起作用。

If you're redirecting somewhere outside of your Rails app, then specifying the protocol as Olly suggests will work.

这篇关于Rails重定向与https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆