Rails: redirect all unknown routes to root_url
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'
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, 2022Comments
-
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 over 13 yearsThanks! Well, it's Rails 2.3.10
-
Albus Dumbledore over 13 yearsThanks 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 over 12 yearsIs this answer for Rails 3 as well?
-
shingara over 12 yearsWorks with rails 3 too. You just need change the ligne with match. See globing in routing guides
-
Avisra over 10 yearsYou can also use
get
instead ofmatch
, which is just shorthand for addingvia: :get
. -
ahnbizcad over 9 yearsMake sure to put this at the bottom of your routes list.
-
ahnbizcad over 9 yearsThank you for the notice of putting it at the bottom.
-
ahnbizcad over 9 yearsThis 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ć about 9 yearsWell 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 about 8 yearsI'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
orget '*path' => redirect('/')
-
Jeremy Thomas about 8 yearsDoes this still work? Doesn't seem to work in my Rails 4 app.
-
tomb almost 6 yearsI 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 over 5 yearsalso, you can add catch-all route after application initialization, as described here: github.com/rails/rails/issues/671#issuecomment-1780159
-
Sergio Gonzalez almost 4 yearsThis answer is the way to go in Rails 6 with ActiveStorage