Alpar
Alpar

Reputation: 2897

How to redirect a URI with a parenthesis in Ruby on Rails

I'm trying to do a redirect in 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)

I tried both URL encoding ) and escaping it \) and \\) but none of that worked. 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.

Upvotes: 0

Views: 156

Answers (1)

Vijay Agrawal
Vijay Agrawal

Reputation: 1683

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)

Upvotes: 1

Related Questions