Rails - 如何从http://example.com重定向到https://www.example.com [英] Rails - How to Redirect from http://example.com to https://www.example.com
问题描述
我正在寻找如何清理我的应用的网址。我的应用程序由Heroku上的Rails 3提供支持。
I'm looking to learn how to cleanup my app's URLs. My app is powered by Rails 3 on Heroku.
所需的URL是 https://www.example.comite.com
我想将与上述不同的所有网址重定向到该网址。这是一个Rails的事情还是DNS?
I'd like to redirect all URLs unlike the above to that URL. Is this a Rails thing or DNS?
错误的URL:
https://example.comite.com
http://www.example.comite.com
http://example.comite.com
如果有任何结尾,如 http://www.example.comite.com/photo/1
为了通过路径重定向网址: https://www.example.comite.com/photo/1
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
推荐答案
DNS记录无法定义域的协议,因此您不能将 http://
重定向到 https://
通过DNS。通过Web服务器配置进行操作不是便携式的,很难做到,容易出错,只是过时。这是Rails路由器最好处理的工作。
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 => "" }
这篇关于Rails - 如何从http://example.com重定向到https://www.example.com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!