Rails:重定向到当前路径但不同的子域 [英] Rails: redirect to current path but different subdomain

查看:15
本文介绍了Rails:重定向到当前路径但不同的子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这应该相当简单,但我不熟悉它是如何完成的...

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/products/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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆