如何在Rails路由中使用正则表达式进行重定向?

编程入门 行业动态 更新时间:2024-10-13 18:19:38
本文介绍了如何在Rails路由中使用正则表达式进行重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的route.rb中尝试重写的规则可能是不言自明的:

This attempted rewrite rule in my routes.rb is probably self-explanatory:

match "/:user/:photo-thumb.png" => redirect("/%{user}/photos/%{photo}/image?style=thumb"), :photo => /[a-zA-Z]+/

我想重定向诸如mysite/alice之类的内容/foo-thumb.png到mysite/alice/photos/foo/image?style=thumb

I want to redirect something like mysite/alice/foo-thumb.png to mysite/alice/photos/foo/image?style=thumb

上述尝试不过是错误的。有任何解决方案吗?

The above attempt is wrong though. Any ideas for fixing it?

推荐答案

以下方法对我有用:

match "/:user/:photo_thumb.png" => redirect{|p| "/#{p[:user]}/photos/#{p[:photo_thumb].split('-')[0]}/image?style=thumb" }, :constraints => { :photo_thumb => /[a-zA-Z]*\-thumb/ }

我很想找到一种更简单的方法。 (正如塔德曼指出的那样,我最好通过nginx重写来做到这一点。请参阅以下相关问题: Routes.rb,机架重写,nginx / apache重写规则)

I'd love to find a simpler way to do it though. (As tadman points out, I may be better off doing this with nginx rewrites. See this related question: Routes.rb vs rack-rewrite vs nginx/apache rewrite rules )

更多推荐

如何在Rails路由中使用正则表达式进行重定向?

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

发布评论

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

>www.elefans.com

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