在Rails 3中没有尾随斜杠的情况下重定向到规范路由

在Rails 3上,我正在尝试从没有尾部斜杠的URL重定向到具有斜杠的规范URL。

match "/test", :to => redirect("/test/") 

但是,上面的路由匹配/ test和/ test /导致重定向循环。

如何使其仅匹配没有斜杠的版本?

您可以在控制器级别强制重定向。

 # File: app/controllers/application_controller.rb class ApplicationController < ActionController::Base protected def force_trailing_slash redirect_to request.original_url + '/' unless request.original_url.match(/\/$/) end end # File: app/controllers/test_controller.rb class TestController < ApplicationController before_filter :force_trailing_slash, only: 'test' # The magic # GET /test/ def test # ... end end 

ActionDispatch中有一个名为trailing_slash的选项,可用于强制URL末尾的尾部斜杠。 我不确定它是否可以在路由定义中使用。

 def tes_trailing_slsh add_host! options = {:controller => 'foo', :trailing_slash => true, :action => 'bar', :id => '33'} assert_equal('http://www.basecamphq.com/foo/bar/33/', W.new.url_for(options) ) end 

在您的情况下,最好的方法是使用Rack或您的Web服务器来执行重定向。 在Apache中,您可以添加诸如的定义

 RewriteEngine on RewriteRule ^(.+[^/])$ $1/ [R=301,L] 

将没有尾部斜杠的所有路由重定向到带有斜杠的相应路径。

或者您可以使用rack-rewrite在Rack级别的Rails应用程序中执行相同的任务。

我想做同样的事情 ,为博客提供一个cannonicalurl,这是有效的

  match 'post/:year/:title', :to => redirect {|env, params| "/post/#{params[:year]}/#{params[:title]}/" }, :constraints => lambda {|r| !r.original_fullpath.end_with?('/')} match 'post/:year/:title(/*file_path)' => 'posts#show', :as => :post, :format => false 

然后我有另一个规则来处理post内的相对路径。 订单很重要,所以前者先行,而通用先行。

也许它适用

 match "/test$", :to => redirect("/test/")