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

编程入门 行业动态 更新时间:2024-10-25 22:28:24
本文介绍了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 / posts / 1 很好,但 blog.myapp/products/123 重定向到 www.myapp/产品/ 123 。

Ie: blog.myapp/posts/1 is fine but blog.myapp/products/123 redirects to www.myapp/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:重定向到当前路径,但不同的子域

本文发布于:2023-11-01 11:27:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549218.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   重定向   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!