如何巧妙应对Rails资源路由中的点号问题?

2026-04-11 15:471阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计291个文字,预计阅读时间需要2分钟。

如何巧妙应对Rails资源路由中的点号问题?

我希望像这样有SEO优化的网址 + http://example.com/sites/1-blau.de/plans,但路径中的点导致Rails陷入困境。如何将点转换成百分比值表示形式,以便它可工作?我的路径:resources :sites, only: []

如何巧妙应对Rails资源路由中的点号问题?

我的代码:rubyresources :sites, only: []

结果:rubyresources :sites, only: []

我希望像这样有SEO优化的网址

example.com/sites/1-blau.de/plans

但路径中的点导致Rails陷入困境.如何将点转换为百分比表示形式,以便它可以工作?

我的路线:

resources :sites, only: [] do resources :plans, only: [:index, :show] do end end

我尝试过URI.escape和CGI.escape,但都没有用.

URI.escape('a.b')=> "a.b" CGI.escape('a.b')=> "a.b"

我以为我想要什么

Foo.escape('a.b')=> "a%2Eb" 使用接受点字符的约束.

get 'sites/:site_name/plans', constraints: { site_name: /[a-zA-Z0-9\.]+/ }

从doc:

By default, dynamic segments don’t accept dots – this is because the dot is used as a separator for formatted routes. If you need to use a dot within a dynamic segment, add a constraint that overrides this – for example, id: /[^/]+/ allows anything except a slash.

本文共计291个文字,预计阅读时间需要2分钟。

如何巧妙应对Rails资源路由中的点号问题?

我希望像这样有SEO优化的网址 + http://example.com/sites/1-blau.de/plans,但路径中的点导致Rails陷入困境。如何将点转换成百分比值表示形式,以便它可工作?我的路径:resources :sites, only: []

如何巧妙应对Rails资源路由中的点号问题?

我的代码:rubyresources :sites, only: []

结果:rubyresources :sites, only: []

我希望像这样有SEO优化的网址

example.com/sites/1-blau.de/plans

但路径中的点导致Rails陷入困境.如何将点转换为百分比表示形式,以便它可以工作?

我的路线:

resources :sites, only: [] do resources :plans, only: [:index, :show] do end end

我尝试过URI.escape和CGI.escape,但都没有用.

URI.escape('a.b')=> "a.b" CGI.escape('a.b')=> "a.b"

我以为我想要什么

Foo.escape('a.b')=> "a%2Eb" 使用接受点字符的约束.

get 'sites/:site_name/plans', constraints: { site_name: /[a-zA-Z0-9\.]+/ }

从doc:

By default, dynamic segments don’t accept dots – this is because the dot is used as a separator for formatted routes. If you need to use a dot within a dynamic segment, add a constraint that overrides this – for example, id: /[^/]+/ allows anything except a slash.