Rails - 如何从 http://example.com 重定向到 https://www.example.com
我正在学习如何清理我的应用程序的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Rails 3.1.0 及更高版本具有
force_ssl
,这是一种控制器方法,可为非开发环境重定向到 https。http://api.rubyonrails.org/classes/ActionController/ForceSSL/ClassMethods.html
地点它位于您想要重定向的每个控制器中,或者更好的是,将其放置在您的 ApplicationController 中:
app/controllers/application.rb:
这是始终包含在您的应用程序中的一件好事(当然,您必须获得一个证书)。 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:
This is a good thing to always include in your apps (and of course you'll have to get a certificate). HTTPS Everywhere!
您始终可以将其放入 Production.rb...
config.use_ssl = true
You can always throw this in your production.rb...
config.use_ssl = true
在您的 vhosts 文件中执行此操作。
设置 SSL 虚拟主机。
在您的标准端口 80 虚拟主机中。将其添加到配置中:
这会将所有端口 80 请求转发到 https。
DO it in your vhosts file.
Setup a SSL vhost.
In your standard port 80 virtual host. Add this to the config:
This will forward all port 80 requests to https.
作为user2100689的答案的扩展,在Rails 3+中,您可以使用
config.force_ssl = true
在 config/environments/production.rb 中,该行可以取消注释,如下所示
As an extension to user2100689's answer, in Rails 3+ you can use
config.force_ssl = true
in config/environments/production.rbThe line can just be uncommented as follows
DNS 记录无法定义域的协议,因此您无法通过 DNS 将
http://
重定向到https://
。通过 Web 服务器配置来完成此操作不可移植、难以实现、容易出错并且已经过时。这项工作最好由 Rails 路由器来处理。DNS records cannot define the protocol for a domain, therefore you can't redirect
http://
tohttps://
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.因为这是 Heroku,所以您不能使用 apache 或 nginx 配置。你可以做的就是在你的 ApplicationController 中放置一个 before_filter ,假设你有 3 个或更多控制器,例如
下面这些,虽然当然它们将位于单独的文件中,但
在使用设计时您可能还需要稍微调整一下路线,但我怀疑这是
就像我们所做的那样,所以我不会在这里讨论这些细节,并且我已经修改了代码
上面以避免这种并发症。
快乐的黑客。
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
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.