如何解决在Ruby on Rails中点字符对资源路由的影响问题?

更新于
2026-10-09 20:23:55
2阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

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

如何解决在Ruby on Rails中点字符对资源路由的影响问题?

我希望有SEO优化的网站,例如:http://example.com/sites/1-blau.de/plans,但路径中的点导致Rails陷入困境。如何将点转换成百分比值,以便于工作?我的路由:resources :sites, only: [] do res resend

我希望像这样有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" 使用接受点字符的约束.

如何解决在Ruby on Rails中点字符对资源路由的影响问题?

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.

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

如何解决在Ruby on Rails中点字符对资源路由的影响问题?

我希望有SEO优化的网站,例如:http://example.com/sites/1-blau.de/plans,但路径中的点导致Rails陷入困境。如何将点转换成百分比值,以便于工作?我的路由:resources :sites, only: [] do res resend

我希望像这样有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" 使用接受点字符的约束.

如何解决在Ruby on Rails中点字符对资源路由的影响问题?

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.