2011-06-04 62 views
0

的routes.rb映射器錯誤:重定向傳統路線呈現耙

Myapp::Application.routes.draw do 
    root :to => "posts#index" 
    match "posts/autocomplete_topic_name", :as => "autocomplete_topic_name" 
    match "/new" => "posts#new", :as => :new 
    # resources 
    resources :topics 
    resources :posts 
    resources :comments 
    # static 
    match "/about" => "pages#about", :as => :about 
    match "/help" => "pages#help", :as => :help 
    match "/home" => "home#index", :as => :home 
    # redirects 
    match "/tags" => redirect("/topics") 
    match "/entries" => redirect("/posts") 
    match "/comments" => redirect("/") 
end 

截至rake routes末的錯誤寫着:

   tags  /tags(.:format)       {:to=>#<Proc:[email protected]/home/basicobject/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.6/lib/action_dispatch/routing/mapper.rb:366 (lambda)>} 
      entries  /entries(.:format)      {:to=>#<Proc:[email protected]/home/basicobject/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.6/lib/action_dispatch/routing/mapper.rb:366 (lambda)>} 
          /comments(.:format)      {:to=>#<Proc:[email protected]/home/basicobject/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.6/lib/action_dispatch/routing/mapper.rb:366 (lambda)>} 

我想一些舊的路徑重定向到新更好的SEO路線。路線如:

/entries/8需要重定向到/posts/8等等。

回答

1

棒這在application_controller.rb

rescue_from ActionController::RoutingError do |exception| 
    flash[:error] = "Sorry, we were not quite sure where you were trying to go" 
    redirect_to root_url 
end 
+0

顯示404而不是重定向到根會更好嗎? PS:謝謝你的迴應。我的下一步是爲所有其他路由錯誤創建一個捕獲所有路由。 – BasicObject 2011-06-04 18:15:22

+0

此問題的作者希望保留舊版網址。投擲404的將少到SEO,將重定向到根。你需要做的是用新的URL返回301。當谷歌遇到301的時候,它會更新它的索引。任何其他可以降低您的搜索引擎優化排名給定的內容。 – Stewart 2013-02-05 04:32:56

0

您需要保留完整的URL,並通過它與任何參數一起。在這種情況下,即使通配符不匹配,您也無法將其傳遞到新路線。

試試這個

match "/entries/:post_id" => redirect("/posts/%{post_id}") 

這將通過您的PARAM以及投擲正確的HTTP錯誤代碼,以便谷歌將更新它的索引。