42

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!

Albus Dumbledore
  • 12,368
  • 23
  • 64
  • 105

5 Answers5

79

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('/')
ldeld
  • 546
  • 3
  • 18
Arkan
  • 6,196
  • 3
  • 38
  • 54
  • Thanks! Well, it's Rails 2.3.10 – Albus Dumbledore Nov 10 '10 at 17:56
  • Thanks a *lot*, man! It did it. That's how I did it in rails 2: `map.connect '*path', :controller => 'home', :action => 'home'` So, it's the `'*path'` that was the key to the whole thing. Thanks again :-) – Albus Dumbledore Nov 10 '10 at 19:07
  • You can also use `get` instead of `match`, which is just shorthand for adding `via: :get`. – Peeja Oct 28 '13 at 21:31
  • 8
    Make sure to put this at the bottom of your routes list. – ahnbizcad Feb 18 '15 at 15:39
  • This doesn't seem to work for valid routes with invalid ids (e.g. `root.com/articles/293`, where 293 is a non-existent article id in your database) – ahnbizcad Feb 24 '15 at 02:59
  • 1
    Well that's because route was matched and that's all that router is concerned about. You need to do additional check in your controller to see if the article with the provided ID exists. – Almir Sarajčić Jun 23 '15 at 08:36
  • I'd like to point out that "or" should not be part of the code block in this answer. Should be `match '*path' => redirect('/'), via: :get` or `get '*path' => redirect('/')` – GameKyuubi Apr 05 '16 at 04:36
21

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?
Sam Wilder
  • 869
  • 1
  • 9
  • 21
11

Rails 4-

(routes.rb)

You can still use a simple get to redirect all unknown routes.

  get '*path', to: 'home#index'

If you wish to provide routing to both POST and GET requests you can still use match, but Rails wants you to specify the request method via via.

  match "*path" => "home#index", via: [:get, :post]  

Remember that routes.rb is executed sequentially (matching the first route that fits the supplied path structure), so put wildcard catching at the bottom of your matchings.

user
  • 3,388
  • 7
  • 33
  • 67
9

There seems to be a bug in rails 5.2 where active_storage routes are picked up by the catchall route, resulting in broken links to uploaded images. The issue has been reported in the rails repo on github, and someone commented with the below patch until the bug gets fixed in a new release:

In routes.rb right before last end

get '*all', to: 'application#index', constraints: lambda { |req|
    req.path.exclude? 'rails/active_storage'
  }

then in the application controller add:

def index
  flash.notice = 'No page found at that address'
  redirect_to root_path
end
tomb
  • 1,374
  • 1
  • 13
  • 23
1

You need create a controller to do that

class RedirectsController 

  def index
    redirect_to root_url
  end
end

And in your routes

map.connect '*', :controller => 'redirects', :action => 'index'
shingara
  • 46,608
  • 11
  • 99
  • 105