我认为这应该相当容易,但我不熟悉它是如何完成的......
你如何编写一个 before 过滤器来检查当前请求是否针对某些子域,如果是,则将其重定向到相同的 url 但位于不同的子域上?
即:blog.myapp.com/posts/1
很好,但blog.myapp.com/products/123
重定向到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,所以最简单的方法是传递一个类似的东西:
redirect_to request.url.sub('blog', 'www')
啊,我不知道这个
.sub
方法存在!谢谢!哦!sub 方法只是普通的旧 ruby sub 方法......好吧,经过进一步的实验,这可行,但我必须使用更多的正则表达式来处理它。该
request.url
对象让我足以弄清楚其余部分,谢谢!