[英]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...我认为这应该相当容易,但我不熟悉它是如何完成的......
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?您如何编写一个 before 过滤器来检查当前请求是否针对某些子域,如果是,则将其重定向到相同的 url 但位于不同的子域上?
Ie: blog.myapp.com/posts/1
is fine but blog.myapp.com/products/123
redirects to www.myapp.com/products/123
.即: blog.myapp.com/posts/1
很好,但blog.myapp.com/products/123
重定向到www.myapp.com/products/123
。
I was thinking something like...我在想像...
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
How do you write that redirect?你怎么写重定向?
ActionController::Redirecting#redirect_to允许你传递一个绝对的 URL,所以最简单的方法是传递一个类似的东西:
redirect_to request.url.sub('blog', 'www')
A simple choice to redirect from subdomain to subdomain would be the following从子域重定向到子域的简单选择如下
redirect_to subdomain: 'www', :controller => 'www', :action => "index"
This would be called on a domain name for example foo.apple.com would then go to www.apple.com这将在域名上调用,例如 foo.apple.com 然后将转到 www.apple.com
无需执行字符串 sub 或指定控制器或动作的更简单的选项是使用
redirect_to url_for(subdomain: 'www', only_path: false)
A simple solution for this is一个简单的解决方案是
redirect_to dummy_rul(subdomain: 'subdomain')
Example:例子:
redirect_to projects_url(subdomain: 'edv') it will redirect on below url
"http://edv.maindomain.com/projects"
"edv" is my subdomain “edv”是我的子域
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.