Rails: redirect all unknown routes to root_url

34,713

Solution 1

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('/')

Solution 2

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?

Solution 3

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.

Solution 4

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

Solution 5

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'
Share:
34,713
Albus Dumbledore
Author by

Albus Dumbledore

For most of my time I do programming stuff, but I like math, too, especially if it’s got a more applied nature. I love jazz music and action-packed thrilling books, where the good guys are noble and able, but sound self-deprecating, and always think coolly and clearly. Most of all, however, I like video games with compelling atmosphere, innovative design and great eye for detail. I am best at Java, but I also have experience with C++, Python, Ruby, Visual Basic, Pascal, ActionScript and PHP. I have some idea of functional programming, too, as I’ve done some good amount of projects in Matlab and Mathematica. I prefer simpler code, but I am not too scared to go deep, if it’s the only option. My love for books and mobile devices has leaded me to making my own ebook reader: The AlbiteREADER. One can find free ebooks there, too. It’s a big thing for me, for I’ve been making the app for over four months. As far as math is concerned, I don’t like it raw, but prefer it in connection with other sciences, i.e. numerical analysis, discreet math, statistics, biomathematics, etc. I’ve done some good amount of math projects with Matlab and Mathematica. I’ve also had the chance to teach biomath as an assistant, i.e. I was responsible for the demonstrational part of the subject. In relation with that, I can say, I wrote some good quantity of Mathematica code and some lesser amount of mathematical stuff.

Updated on July 09, 2022

Comments

  • Albus Dumbledore
    Albus Dumbledore almost 2 years

    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
    Albus Dumbledore over 13 years
    Thanks! Well, it's Rails 2.3.10
  • Albus Dumbledore
    Albus Dumbledore over 13 years
    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 :-)
  • Sam Wilder
    Sam Wilder over 12 years
    Is this answer for Rails 3 as well?
  • shingara
    shingara over 12 years
    Works with rails 3 too. You just need change the ligne with match. See globing in routing guides
  • Avisra
    Avisra over 10 years
    You can also use get instead of match, which is just shorthand for adding via: :get.
  • ahnbizcad
    ahnbizcad over 9 years
    Make sure to put this at the bottom of your routes list.
  • ahnbizcad
    ahnbizcad over 9 years
    Thank you for the notice of putting it at the bottom.
  • ahnbizcad
    ahnbizcad over 9 years
    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)
  • Almir Sarajčić
    Almir Sarajčić about 9 years
    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.
  • GameKyuubi
    GameKyuubi about 8 years
    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('/')
  • Jeremy Thomas
    Jeremy Thomas about 8 years
    Does this still work? Doesn't seem to work in my Rails 4 app.
  • tomb
    tomb almost 6 years
    I have a problem in rails 5 where the get '*path', to: 'searches#new' results in 406 errors for all uploaded images in my app.
  • Zoran Majstorovic
    Zoran Majstorovic over 5 years
    also, you can add catch-all route after application initialization, as described here: github.com/rails/rails/issues/671#issuecomment-1780159
  • Sergio Gonzalez
    Sergio Gonzalez almost 4 years
    This answer is the way to go in Rails 6 with ActiveStorage