Class: Rackables::TrailingSlashRedirect

Inherits:
Object
  • Object
show all
Defined in:
lib/rackables/trailing_slash_redirect.rb

Overview

Request paths with a trailing slash are 301 redirected to the version without, e.g.:

GET /foo/   # => 301 redirects to /foo

Constant Summary collapse

HAS_TRAILING_SLASH =
%r{^/(.*)/$}

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ TrailingSlashRedirect

Returns a new instance of TrailingSlashRedirect.



10
11
12
# File 'lib/rackables/trailing_slash_redirect.rb', line 10

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/rackables/trailing_slash_redirect.rb', line 14

def call(env)
  req = Rack::Request.new(env)
  if req.path_info =~ HAS_TRAILING_SLASH
    req.path_info = "/#{$1}"
    [301, {"Location" => req.url}, []]
  else
    @app.call(env)
  end
end