Redirect_from BS?


#1

I’ve notice some entries in the log where folks are just poking around.
Is it possible to ‘trap’ these and redirect_to something valid like the
site’s home page?

Thanks,
Bill


#2

Bill W. wrote:

I’ve notice some entries in the log where folks are just poking around.
Is it possible to ‘trap’ these and redirect_to something valid like the
site’s home page?

Sure. I’m working on a site right the requirements to visit a specified
page can be complex. I use something at the top of each method like

bad_action and return if …

where the “if” clause includes a set of conditions anybody visiting a
specific page must meet (@user_logged_in, !params[‘id’].blank?, etc.)

bad_action simply redirects to the index page or, if the user is logged
in, to the user home page.

–Al Evans


#3

Bill W. wrote:

I’ve got before_filters in place for that sort of thing. What I was
thinking / wondering about is something at a higher level that would
catch
attempts to invoke controllers / actions that don’t exist. I guess I’m
asking 'is there some way to redirect to a valid page (like the site’s
home
page) rather than just throwing a 404 ‘Page not found’?

I think you can do…

map.connect ‘’, :controller => “whatever”, :action => “home”

…at the end of your routes and catch all unrecognized
controllers/actions. I haven’t had occasion to do this, though. I don’t
think it would work for the case where the controller is recognized, but
the action isn’t.

–Al Evans


#4

Hi Al,

Thanks for your reply. More below.

Al Evans wrote:

Bill W. wrote:

I’ve notice some entries in the log where folks are just poking around.
Is it possible to ‘trap’ these and redirect_to something valid like the
site’s home page?

Sure. I’m working on a site right the requirements to visit a specified
page can be complex. I use something at the top of each method like

bad_action and return if …

I’ve got before_filters in place for that sort of thing. What I was
thinking / wondering about is something at a higher level that would
catch
attempts to invoke controllers / actions that don’t exist. I guess I’m
asking 'is there some way to redirect to a valid page (like the site’s
home
page) rather than just throwing a 404 ‘Page not found’?

I’m guessing I should have marked this [OT] now that I think about it.
It’s
probably something that has to happen outside the Rails app. I just
don’t
know enough about the architecture to know where the ‘trap’ should be
set.
Where / what decides that a 404 error gets thrown and that it should
display
404.html? Is it configurable so that I could set it to display my home
page
instead?

Thanks,
Bill