ruby - Rails redirecting invalid route to root -
if website www.foo.com if user types www.foo.com/blahblahblah /blahblahblah invalid path (obviously). want instead redirect root_path controller can process url -- page www.foo.com should rendered want pull text blahblahblah , it. how do this?
there several possibilities. here's one. add bottom of routes.rb:
match ':not_found' => 'my_controller#index', :constraints => { :not_found => /.*/ } which establish catch-all route make mycontroller's index action handle missing paths; can detect them looking @ params[:not_found] , doing whatever wants, such redirecting root_path (redirect_to root_url), redirecting somewhere strategically based on bad path, rendering special, examining referrer/referer clues source, etc.
the :constraints option necessary; otherwise not_found param won't able contain special characters slashes , dots.
put @ bottom of routes because, obviously, match everything, , want give other routes first crack @ path.
if want redirect, nothing more, instead (again, @ bottom):
match ':not_found' => redirect('/'), :constraints => { :not_found => /.*/ }
Comments
Post a Comment