10

How can I redirect incorrect url to 404 page in routes.rb? Now I use 2 examples code:

# example 1
match "/go/(*url)", to: redirect { |params, request| Addressable::URI.heuristic_parse(params[:url]).to_s }, as: :redirect, format: false

# example 2
match "/go/(*url)", to: redirect { |params, request| Addressable::URI.heuristic_parse(URI.encode(params[:url])).to_s }, as: :redirect, format: false

But when I try using russian words in 'url' parameter, in 1st example I get 500 page (bad URI), in 2nd - I get redirect to stage.example.xn--org-yedaaa1fbbb/

Thanks

dnch
  • 9,565
  • 2
  • 38
  • 41
piton4eg
  • 1,096
  • 2
  • 10
  • 21

2 Answers2

30

If you want custom error pages, you'll be best looking at this answer I wrote a few weeks ago


You need several important elements to create custom error routes:

-> Add custom error handler in application.rb:

# File: config/application.rb
config.exceptions_app = self.routes

-> Create /404 routes in your routes.rb:

# File: config/routes.rb
if Rails.env.production?
   get '404', :to => 'application#page_not_found'
end

-> Add actions to application controller to handle these routes

# File: app/controllers/application_controller.rb
def page_not_found
    respond_to do |format|
      format.html { render template: 'errors/not_found_error', layout: 'layouts/application', status: 404 }
      format.all  { render nothing: true, status: 404 }
    end
  end

This is obviously relatively basic, but hopefully it will give you some more ideas on what you can do

Eduardo
  • 508
  • 6
  • 18
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
5

The simplest thing to do is to make sure your routes do not match bad URLs. By default Rails will return a 404 for routes that do not exist.

If you can't do this the default 404 page lives at /404 so you can redirect to that location. Something to keep in mind here, however, is that this type of redirect will perform a 301 Permanent Redirect instead of a 302. This may not be the behavior that you want. To do this you can do the following:

match "/go/(*url)", to: redirect('/404')

Instead, I'd recommend setting up a before filter in your action that instead raises a not found exception. I'm not exactly sure if this exception is in the same place in Rails 4, but with Rails 3.2 I'm currently using:

raise ActionController::RoutingError.new('Not Found')

You can then do any processing and URL checking in the controller (if complicated checking of the URL format is required).

cassanego
  • 454
  • 4
  • 5