ruby-on-rails – 使用routes.rb重定向旧URL的Rails
我有一个用Coldfusion构建的旧站点,这是一个用Rails构建的新站点.我想将旧网址重定向到新网址.我不确定路线是否可行(我是菜鸟).这些网址非常相似.这应该很容易,但我不确定最好的方法.
旧网址: mysite.com/this-is-the-slug-right-here/ 新网址: mysite.com/blog/this-is-the-slug-right-here 这是问题,我有3个“内容类型”.旧网站网址没有区分内容类型.新的Rails站点为每种内容类型都有一个控制器:博客,照片,移动照片. 因此,在上面的示例中,/ blog /是控制器(内容类型),这是-slug-right-这里是内容的永久链接或slug.我得到的是这样的: @content = Content.where(:permalink => params[:id]).first 我应该使用routes.rb,还是需要某种catch-all脚本?让我指出正确方向的任何帮助将不胜感激. 编辑以进一步澄清 这是一篇博文:http://jyoseph.com/treadmill-desk-walk-this-way/ 这个新的URL将是/ blog / treadmill-desk-walk-this-way,因为它是一种内容类型的博客. 照片帖子:http://jyoseph.com/berries/ 这个的新URL将是/ photos / berries,因为它是一种内容类型的照片. 内容类型是内容模型上的属性,存储在属性content_type中. 这是我的routes.rb文件: resources :contents match 'mophoblog/:id',:to => 'mophoblog#show' match 'photos/:id',:to => 'photos#show' match 'blog/:id',:to => 'blog#show' root :to => "index#index" match ':controller(/:action(/:id(.:format)))' 用@ mark的答案得到它,这就是我最终的结果. 在我的routes.rb match ':id' => 'contents#redirect',:via => :get,:as => :id 在我的内容控制器中: def redirect @content = Content.where(:permalink => params[:id]).first if @content.content_type.eql?('Photo') redirect_to "/photos/#{@content.permalink}",:status => :moved_permanently elsif @content.content_type.eql?('Blog') redirect_to "/blog/#{@content.permalink}",:status => :moved_permanently elsif @content.content_type.eql?('MoPhoBlog') redirect_to "/mophoblog/#{@content.permalink}",:status => :moved_permanently end end 我确信这可以改进,特别是我重定向的方式,但这完全解决了我的问题. 解决方法
你不能使用routes.rb来做到这一点,但它足够简单,可以设置路由,获取内容类型和重定向.
就像是: routes.rb match.resources :photos match.resources :mobile_photos match.resources :blog #everything_else all resource and named routes before match ':article_id' => 'articles#redirect',:as => :article_redirect #articles_controller.rb def redirect @content = Content.find params[:id] if @content.content_type.eql?('photo') redirect_to photo_path(@content),:status => :moved_permanently elsif @content.content_type.eql?('mobile_photo') redirect_to mobile_photo_path(@content),:status => :moved_permanently ... end 现在它发生在我写这篇文章时你可能只想要一个控制器用于所有这些? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |