Redirect to canonical route in without trailing slash in Rails 3
Asked Answered
S

5

4

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/")

However, the route above matches both /test and /test/ causing a redirect loop.

How do I make it match only the version without the slash?

Snack answered 21/12, 2011 at 12:30 Comment(0)
K
6

You can force the redirect at the controller level.

# 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
Kati answered 22/1, 2014 at 3:45 Comment(1)
original_url also includes query parameters, so this check catches too much.Kobi
A
3

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.

Aniline answered 11/9, 2012 at 6:36 Comment(0)
C
2

There is an option in ActionDispatch called trailing_slash you can use to force the trailing slash at the end of the URL. I'm not sure if it can be used in the routing definition.

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

In your case, the best way is to use Rack or your web server to execute the redirect. In Apache, you can add a definition such as

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

To redirect all routes without a trailing slash to the corresponding one with trailing slash.

Or you can use rack-rewrite to perform the same task in your Rails app at Rack level.

Clockwork answered 21/12, 2011 at 13:25 Comment(2)
rack-rewrite is an interesting option. Though if at all possible I'd prefer a solution in Rails without using additional middleware and without doing it on the webserver side.Snack
Actually, when you call redirect("/test/") you're using a Rack middleware. ;)Clockwork
D
1

Bullet-proof solution:

  before_action :force_trailing_slash

  ...

  private

  def force_trailing_slash
    return if trailing_slash?

    url = url_for \
      request.path_parameters
        .merge(request.query_parameters)
        .merge(trailing_slash: true)

    redirect_to url, status: :moved_permanently
  end

  def trailing_slash?
    URI(request.original_url).path.ends_with? '/'
  end
Divan answered 25/2, 2021 at 16:34 Comment(0)
R
0

Maybe it works with

match "/test$", :to => redirect("/test/")
Reeta answered 21/12, 2011 at 13:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.