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?
Ie: blog.myapp.com/posts/1
is fine but blog.myapp.com/products/123
redirects to 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
无需执行字符串 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
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.