Redirect to canonical route in without trailing slash in Rails 3

前端 未结 4 1510
你的背包
你的背包 2021-01-19 15:46

On Rails 3, I\'m trying to redirect from a URL without a trailing slash to the canonical URL that has a slash.

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


        
4条回答
  •  迷失自我
    2021-01-19 16:04

    I wanted to do the same to have a cannonical url for a blog, this works

      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
    

    then I have another rule which deals with the relative paths inside the post. Order is important, so former goes first and generic one goes second.

提交回复
热议问题