【发布时间】:2011-02-21 21:57:20
【问题描述】:
我对一个相当普遍的问题的看法略有不同:对 SEO 友好的 URL。我有一个 PagesController,所以我的 URL 目前是这样的(使用 restful 路由):
/pages/some-content-title
这很好用,但是页面有层次结构,所以我需要以下内容:
/some-content-title 路由到 /pages/some-content-title
我也可以使用:
match '*a', :to => 'errors#routing'
在我的 routes.rb 中并将其捕获在 ErrorsController 中:
class ErrorsController < ApplicationController
def routing
Rails.logger.debug "routing error caught looking up #{params[:a]}"
if p = Page.find_by_slug(params[:a])
redirect_to(:controller => 'pages', :action => 'show', :id => p)
return
end
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
end
我的问题在于希望 SEO 消除 URL 的“pages/”部分。 SEO-dude 想要什么(这里有个例子很关键):
/insurance => :controller=>'pages', :id=>'insurance' # 但是地址栏的url是/insurance
/insurance/car :controller=>'pages', :category=>'insurance', :id=>'car' # 但是地址栏的url是/insurance/car
是否有一种通用的方式让他获得他对 Google 的喜爱并让我保持路线正常?
谢谢!
【问题讨论】:
标签: ruby-on-rails routing