Rails:重定向到当前路径,但不同的子域 [英] Rails: redirect to current path but different subdomain
问题描述
我认为这应该很容易,但是我不熟悉它的完成方式...
I think this should be fairly easy but I'm not familiar with how it's done...
您如何编写一个before过滤器来检查是否当前请求是针对某些子域的,如果是这样,将其重定向到相同的URL,但在不同的子域上?
How do you write a before filter that would check if the current request is for certain subdomains, and if so redirect it to the same url but on a different subdomain?
即: blog.myapp.com / posts / 1
很好,但 blog.myapp.com/products/123
重定向到 www.myapp.com/产品/ 123
。
Ie: blog.myapp.com/posts/1
is fine but blog.myapp.com/products/123
redirects to www.myapp.com/products/123
.
我在想类似...
class ApplicationController < ActionController::Base
before_filter :ensure_domain
protected
def ensure_domain
if request.subdomain == 'blog' && controller_name != 'posts'
redirect_to # the same url but at the 'www' subdomain...
end
end
end
如何编写该重定向?
推荐答案
ActionController :: Redirecting#redirect_to 允许您传递绝对URL,因此最简单的方法是要做的就是通过以下方式传递它:
ActionController::Redirecting#redirect_to allows you to pass an absolute URL, so the easiest thing to do would be to pass it one with something like:
redirect_to request.url.sub('blog', 'www')
这篇关于Rails:重定向到当前路径,但不同的子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!