Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: redirect all unknown routes to root_url

Whenever a user hits the wrong page, rails shows 404.html from the public folder. However, I'd like just to redirect the browser to the root page, without showing anything. So I tried globbing, but came to no avail, it still shows the 404 page. Here's an extract from my routes file:

# ... map.root :controller => 'home', :action => 'home' map.connect '*', :controller => 'home', :action => 'home' 

Any suggestions? Thanks, guys!

like image 755
Albus Dumbledore Avatar asked Nov 09 '10 09:11

Albus Dumbledore


2 Answers

If your project is powered by rails 3, add simply this line to your routes.rb

match '*path' => redirect('/') 

Edit: If you're on Rails 4 or 5

match '*path' => redirect('/'), via: :get 

or

get '*path' => redirect('/') 
like image 183
Arkan Avatar answered Sep 29 '22 19:09

Arkan


Like the answer by Arkan. One point, if do not want this behaviour in development environment, then could do -

match '*path' => redirect('/')   unless Rails.env.development? 
like image 38
Sam Wilder Avatar answered Sep 29 '22 19:09

Sam Wilder