简体   繁体   English

Rails-仅重定向到1页的子域

[英]Rails - Redirect to Subdomain for 1 Page Only

I am trying to use Rails 4 routing to redirect to a particular subdomain ("secure") for one page only (the shopping cart). 我正在尝试使用Rails 4路由将其重定向到仅一页(购物车)的特定子域(“安全”)。 (The reason I need to do this is that the SSL certificate is available only on the secure subdomain.) Currently, I have the following: (我需要这样做的原因是SSL证书仅在安全子域上可用。)当前,我具有以下内容:

  get '/cart' => redirect { |p, req| req.url.sub('http:// site.com', 'http://secure.site.com') }, :constraints => { :host => 'site.com' }

This works, but then every subsequent link that the user clicks on retains the secure subdomain when I'd like it to default back to the root domain (site.com; no www). 这行得通,但是当我希望它默认返回到根域时,用户单击的每个后续链接都会保留该安全子域(site.com;无www)。

What's the best way to achieve this? 实现此目标的最佳方法是什么?

The reason for that is that usually all link href attributes are relative paths, so '/controller/action', not ' http://example.com/controller/action '. 这样做的原因是通常所有链接href属性都是相对路径,所以是'/ controller / action'而不是' http://example.com/controller/action '。 The relative path means that the same (sub)domain is assumed. 相对路径意味着假定相同的(子)域。

You could change all the links to use _url instead of _path, but I would recommend against it. 您可以将所有链接更改为使用_url而不是_path,但是我建议不要这样做。

Instead I would suggest to write a rack middleware and do redirection there based on the full path or handle it at the application server (nginx, apache etc) configuration level. 相反,我建议编写一个机架中间件,然后根据完整路径进行重定向,或者在应用程序服务器(nginx,apache等)配置级别进行处理。

Or even better, why don't you have all your links under https? 甚至更好,为什么不将所有链接都放在https下? Sounds like the best way to me:) 听起来对我来说是最好的方法:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM