The simplest task is to make sure your routes do not match bad URLs. By default, Rails will return 404 for routes that do not exist.
If you cannot do this, the 404 page is located at /404 by default, so you can redirect to that location. However, here it should be borne in mind that this type of redirect will perform 301 Permanent redirects instead of 302. This may not be the behavior you want. To do this, you can do the following:
match "/go/(*url)", to: redirect('/404')
Instead, I would recommend setting up a filter in my action before that, which instead throws an exception not found. I'm not quite sure that this exception is in the same place in Rails 4, but with Rails 3.2 I currently use:
raise ActionController::RoutingError.new('Not Found')
Then you can perform any processing and verification of URLs in the controller (if complex verification of the URL format is required).
source share