# Redirect\_from BS?

**URL:** <https://www.ruby-forum.com/t/redirect-from-bs/87590>\
**Category:** Rails\
**Created:** [March 4, 2007, 4:40am UTC](https://www.ruby-forum.com/t/redirect-from-bs/87590 "2007-03-04T04:40:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [March 4, 2007, 4:40am UTC](https://www.ruby-forum.com/t/redirect-from-bs/87590/1 "2007-03-04T04:40:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [March 4, 2007, 2:30pm UTC](https://www.ruby-forum.com/t/redirect-from-bs/87590/2 "2007-03-04T14:30:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [March 4, 2007, 7:14pm UTC](https://www.ruby-forum.com/t/redirect-from-bs/87590/3 "2007-03-04T19:14:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [March 4, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/redirect-from-bs/87590/4 "2007-03-04T16:02:06Z")

</div>

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
