gpt4 book ai didi

ruby-on-rails - Rails:重定向到当前路径,但不同的子域

转载 作者:行者123 更新时间:2023-12-03 14:50:39 24 4
gpt4 key购买 nike

我认为这应该很容易,但是我不知道它是如何完成的...

如何编写一个前置过滤器,以检查当前请求是否针对某些子域,如果是,则将其重定向到相同的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,因此最简单的方法是使用类似以下内容的URL进行传递:

redirect_to request.url.sub('blog', 'www')

关于ruby-on-rails - Rails:重定向到当前路径,但不同的子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10594155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com