Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect All Routing Errors to Root URL of the Application

For example typing: localhost:3000/absurd-non-existing-route How do I get invalid routes to point to the main page of the application in Rails?

like image 972
fenec Avatar asked Jun 07 '10 03:06

fenec


2 Answers

The solution presented here works pretty well

#Last route in routes.rb
match '*a', :to => 'errors#routing'

# errors_controller.rb
class ErrorsController < ApplicationController
  def routing
    render_404
  end
end
like image 92
Ryenski Avatar answered Oct 13 '22 18:10

Ryenski


On Rails 4+

Edit your routes.rb file by adding a get "*path", to: redirect('/') line just above the last end as mentioned by user @ Rails: redirect all unknown routes to root_url

like image 6
Epigene Avatar answered Oct 13 '22 17:10

Epigene