Re: Rack middleware to 301 urls with trailing slashes

Refactoring of: Rack middleware to 301 urls with trailing slashes

Language: Ruby

# Modified from the original to store the regexp in a constant, to simplify 
# the removable of the trailing slash, and to utilize Rack to rebuild the URL.

module Rack
  class TrailingSlash

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

    def initialize(app)
      @app = app
    end

    def call(env)
      if env['PATH_INFO'] =~ HAS_TRAILING_SLASH
        env['PATH_INFO'].chomp!('/')
        [301, {"Location" => Rack::Request.new(env).url}, []]
      else
        @app.call(env)
      end
    end

  end
end

# NOTE: real-world experience shows this may not work if used behind Varnish, 
# because the port will be changed due to the proxy_pass...
Reveal More
Added over 2 years ago by Costco_normal trevorturk