2014-01-14 45 views
1

我有一个用PHP编写的应用程序,现在新的应用程序是用Rails编写的。他们忘记了书签和浏览器缓存页面,所以我们仍然在/index.php上获得很多点击量重定向中断到根的链接

我该如何将它指向我的rails应用程序的根目录?

www.mydomain.com/index.php => www.mydomain.com

在此先感谢

+0

您可以在路线中使用匹配 'match“/index.php”,:to =>“somecontroller#action”,:via =>“get” – jbmyid

回答

1

您需要捕获异常,并重定向如果错误代码是404

有关详细信息,请参阅此导视广播: http://railscasts.com/episodes/53-handling-exceptions-revised?view=asciicast

基本信息如下。

config/application.rb路由例外添加到您的应用程序:

config.exceptions_app = self.routes 

创建errors_controller,然后在routes.rb映射文件的错误,而它的表演动作:

match ':status', to: 'errors#show', constraints: {status: /\d{3}/ } 

errors_controller.rb

class ErrorsController < ApplicationController 
    def show 
    if request.path == '/404' 
     redirect_to root_path 
    else 
     render text: request.path 
    end 
    end 
end 

然后使用您的展示模板为那些不是404错误的应用程序渲染一些明智的错误。

1

把这个在您的config/routes.rb所有其他路线后:

matches '*any_path' => redirect('/') 

这将匹配任何给定的URL,并将其重定向到'/'。但只要此声明在routes.rb中最后一次,它将不会用于文件中较高的路线。