简体   繁体   中英

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). (The reason I need to do this is that the SSL certificate is available only on the secure subdomain.) Currently, I have the following:

  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).

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 '. 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.

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.

Or even better, why don't you have all your links under https? Sounds like the best way to me:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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