重定向“myapp.com” 至“www.myapp.com” 在 Rails 中不使用 htaccess?
使用 Morph Labs 的 Appspace 部署站点意味着无法自动将“myapp.com”重定向到“www.myapp.com”(并且无法访问 .htacess)。
有没有一种轨道内的方法可以做到这一点? 我需要像 subdomain-fu 这样的插件吗?
更具体地说,我正在尝试执行以下操作:
- 'myapp.com' => 'www.myapp.com'
- 'myapp.com/session/new' => 'www.myapp.com/session/new'
基本上,我总是希望在每个请求前面加上 'www' 子域(因为 SSL 证书特别有一个通用名称 'www.myapp.com')。
Using Morph Labs' Appspace to deploy a site means no automated way to redirect 'myapp.com' to 'www.myapp.com' (and no access to .htacess).
Is there an in-rails way to do this? Would I need a plugin like subdomain-fu?
More specifically, I'm trying to do something like:
- 'myapp.com' => 'www.myapp.com'
- 'myapp.com/session/new' => 'www.myapp.com/session/new'
Basically, I always want the 'www' subdomain prepended on every request (because the SSL cert specifically has a common name of 'www.myapp.com').
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我知道这个问题已经得到解答,但我认为其他人都应该了解 CodeRack:Canonical Host 解决方案。 这真的很好,因为它允许特定于环境的重定向。 http://coderack.org/users/tylerhunt/middlewares/6-canonical-主机
I know this is answered, but I thought everyone else should know about the CodeRack: Canonical Host solution. This is really nice as it allows for env specific redirects. http://coderack.org/users/tylerhunt/middlewares/6-canonical-host
这里有几种不同的方法:
另一种:
Here is a couple of different ways:
another:
对于那些希望使用 Heroku 强制使用 SSL 的人来说,这对我来说效果很好,基于 根域上的 Heroku SSL
在我的 DNS 设置中,我设置了一个 URL/转发记录(DNS 简单)
CNAME 设置只需要为 WWW 设置
我还必须为我的根设置和别名
然后我决定简单地将 foo.com 替换为环境变量
ENV['SITE_HOST']
(其中 SITE_HOST 可能等于 www.foo.com 或 test.foo.com),以便我可以进行控制通过我的 heroku 配置。 这样,我就可以控制不同环境中发生的事情。 (要在本地设置环境变量,请参阅 https://github.com/bkeepers/dotenv) ,我的测试应用程序使用 test.foo.com 作为 url,它也有自己的 SSL 端点,因此对我来说效果很好。
从现在开始,最终用户将始终使用强制 SSL 访问 www。 旧的链接会出现轻微的挂起,但不会引起任何明显的影响。
For those of you who are looking to also force SSL using heroku, this worked well for me, based on Heroku SSL on root domain
In my DNS settings I set up a URL / Forward record (DNS Simple)
The CNAME setup only needs to be setup for WWW
I also had to setup and Alias for my root
Then I decided to simply replace foo.com with an env variable
ENV['SITE_HOST']
(where SITE_HOST might equal www.foo.com or test.foo.com) so I can have control via my heroku configuration. That way, I can control what happens in different environments. (for setting up env variables locally see https://github.com/bkeepers/dotenv)For example, my test app uses test.foo.com as the url it also has its own SSL endpoint so that works fine for me.
From now on, end users will always access www with forced SSL. Old links will suffer a small hang but nothing noticeable.
我必须更改 Carson 的答案才能使其在 Rails 3 中工作。我将 request.uri 替换为 request.fullpath:
I had to change Carson's answer to get this to work in Rails 3. I replaced request.uri with request.fullpath:
这对我来说非常有用。 我确实做了一点补充,因为我只希望在生产环境中出现这种行为:
This worked great for me. I did make one small addition as I only wanted this behavior in my production environment:
卡森的回答非常有效。
这是相反的代码(www -> 无 www)
Carson's answer works great.
Here's the code to go the other way (www -> no www)
也许这样的事情可以解决问题:
Maybe something like this would do the trick: