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

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -