ruby/rails redirect_to 404

51 篇文章 0 订阅

What’s the 404 error pages and the page role?

 

The 404 error pages’mean

404http协议所定义的一种协议名称,也可说是一种形为,即 服务器找不到请求的网页。例如,对于服务器上不存在的网页经常会返回此代码。从某种意义上来讲,只是一种名称。当然,http协议还规定了其它多种协议名称,最常用500

 

百度百科:404W3C规定的HTTP状态码中的一种错误,是WWW网站访问比较经常出现的错误。最常见的出错提示:404 NOT FOUND404页面就是当用户输入了错误的链接时,返回的页面。404页面的目的是:告诉浏览者其所请求的页面不存在或链接错误,同时引导用户使用网站其他页面而不是关闭窗口离开。

 

The page role

告诉浏览者其所请求的页面不存在或链接错误,同时引导用户使用网站其他页面而不是关闭窗口离开。

 

 

How to make 404

 

First :use the apache server

 

需要准备:

1,自定义的404页面,自己设计,仅能使用后缀名为html的页面

2,找到apache运行的配置文件httpd.conf

3, 修改后需要重启apache服务,因此请提前做好准备

发散思维:

1,可以自定义500的错误信息提示,而不是将异常简单的抛出

2, 将各种错误信息进行客制化,既安全又显得友好。

步骤:

1,将自定义的404页面部署到apache的根目录下(htdocs下面)

     需要通过浏览器确认该页面是否可以正常显示,路径是否正确。

     该处不成功,请不要进下面的步骤。

     验证网页是否可以正常显示:

2,修改httpd.conf配置文件

  #ErrorDocument 404 /404.html 去掉# 该页面默认放置在根目录下面

  可以修改404.html的名称为 xxxx.htmlxxxx为合法的任意名称)

  如果找不到该项,可以在文本中搜索404即可。

  另外一种不推荐的实现形式:ErrorDocument                       

  ErrorDocument 404    (注释2:不推荐的原因)

3,在和系统和网站管理员协调之后重启apache服务。

   正常情况下,重启apache服务可在瞬间完成(五秒钟之内)

不正常时

找一下AllowOverride看看,AllowOverride None改为AllowOverride All

AllowOverride All,建议在网页根目录用.htaccess文件控制
.htaccess
内容如下

ErrorDocument 404 /404.html

 

注意:

1.     切记不要将404错误转向到网站主页,否则可能会导致主页在搜索引擎中消失。如果一个 404 页面的内容小于 512bIE 会认为该 404 页面不够友好,在 IE 下将不会成功返回该 404 错误页面。

2.     切记不要使用绝对URL。例如:http://www.yaiba.net/404.html。正常情况返回的是 404 状态码,而使用URL形式则返回的是 200 状态码。

 

 

 

 

Use rails code

 

使用rails code 方法很多种,有自定义方法加路由控件

http://brian.pontarelli.com/2007/01/14/handling-rails-404-and-500-errors

http://dev.mensfeld.pl/2010/01/handling-custom-404-and-500-errors-in-rails-2/

   大概过程:

l  Edit your app/controllers/application.rb file and add some methods

l  Edit config/routes.rb

l  Edit config/environments/development.rb

 

   最终总结:大都数类似方法都用到ActionController::UnknownAction

           意思为全局捕捉。

           

直接用以下三点:

Edit in application with these code          

rescue_from ActionController::UnknownAction, :with => :render_not_found

def render_not_found

    render :template => "/error_pages/rescue_404.html.erb", :status => 404

    #注意  render :template =>,page大于1024b,否则在IE中会显示IE自己的HTTP 404 page,改成redirect_to 不会出现

end

 

路由:map.connect '*path', :controller => ' error_pages ', :action => ' rescue_404' unless ::ActionController::Base.consider_all_requests_local==false

Or  map.connect '*path', :controller => '
error_pages ', :action => ' rescue_404'
注意路由可以不配制

注:config.action_controller.consider_all_requests_local = true

    表示用户的浏览器上可以看到所有异常错误堆栈。

 

 

  rescue_from Exception, :with => :render_error  

   #500 页面错误
  rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found  

   #rails记录无
  rescue_from ActionController::RoutingError, :with => :render_not_found

   #rails路由出错
  rescue_from ActionController::UnknownController, :with => :render_not_found

    #rails controller出错
  # customize these as much as you want, ie, different for every error or all the same
  rescue_from ActionController::UnknownAction, :with => :render_not_found

   #  rails action出错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值