简体   繁体   English

如何在Ruby on Rails中使用括号将URI重定向

[英]How to redirect a URI with a parenthesis in Ruby on Rails

I'm trying to do a redirect in Rails 4 我正在尝试在Rails 4中进行重定向

get '/something)', to: redirect("/something", status: 301), format: false

This doesn't work because parentheses are used for optional parameters 这是行不通的,因为括号用于可选参数

The error is: Racc::ParseError parse error on value ")" (RPAREN) 错误是: Racc::ParseError parse error on value ")" (RPAREN)

I tried both URL encoding ) and escaping it \\) and \\\\) but none of that worked. 我尝试了两种URL编码)并转义了\\)\\\\)但没有一个起作用。 From my searches it seems that nobody ran into it before. 从我的搜索看来,以前没有人遇到过。 It's rather strange indeed, but someone added the parentheses by mistake when linking to my site and now I want to redirect visitors to the correct one instead of the 404. 确实确实很奇怪,但是有人在链接到我的网站时错误地添加了括号,现在我想将访问者重定向到正确的地址而不是404。

You can do this, 你可以这样做,

constraints(path: /something\)/)  do 
  get '/:path', to: redirect("/something", status: 301), format: false
end

But ideal way is to put this redirection in you web server (eg: nginx) 但是理想的方法是将此重定向放置在您的Web服务器中(例如:nginx)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM