是否可以将这些结合到一个陈述中?
match '/:action', to: redirect('/'), constraints: {action: /properties/}, via: :all
match '/:action/*other', to: redirect('/'), constraints: {action: /properties/}, via: :all
如果用户输入的根网址以/properties
开头(例如www.google.com/properties
,www.google.com/properties/foo/bar
,www.google.com/properties/1/bar/foo/baz/test
),我想重定向到根网址。所有这些示例都应该回到www.google.com
。
修改
我最终得到了match '/:action(/*anything)', to: redirect('/'), constraints: {action: /properties/}, via: :all
答案 0 :(得分:2)
让它们成为可选项。
match '/:action(/:other)', to: redirect('/'), constraints: {action: /properties/, other: [a-zA-Z0-9\/\\]+ }, via: :all
来自doc:
get ':controller(/:action(/:id))'
此路由还会将/ photos的传入请求路由到 PhotosController #index,因为:action和:id是可选参数, 用括号表示。