Sha256: e9610f0b980e2086d5df832dc95f10ae4b2fe8fc330ce898e35daf9959af52ca

Contents?: true

Size: 1019 Bytes

Versions: 22

Compression:

Stored size: 1019 Bytes

Contents

module Locomotive
  module Middlewares
    class SeoTrailingSlash

      def initialize(app, opts = {})
        @app = app
      end

      def call(env)
        path, query = env['PATH_INFO'], env['QUERY_STRING']

        if !path.starts_with?("#{Locomotive.mounted_on}/") && (match = path.match(%r{(.+)/$}))
          url = self.redirect_url(match[1], query)
          self.redirect_to(url)
        else
          @app.call(env)
        end
      end

      protected

      # Create a 301 response and set it up accordingly.
      #
      # @params [ String ] url The url for the redirection
      #
      # @return [ Array ] It has the 3 parameters (status, header, body)
      #
      def redirect_to(url)
        response = Rack::Response.new
        response.redirect(url, 301) # moved permanently
        response.finish
        response.to_a
      end

      def redirect_url(base, query)
        if query.blank?
          base
        else
          "#{base}?#{query}"
        end
      end

    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
locomotive_cms-2.5.5 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.5.4 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.5.3 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.5.2 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.5.1 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.5.0 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.5.0.rc3 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.5.0.rc2 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.5.0.rc1 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.4.1 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.4.0 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.3.1 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.3.0 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.2.3 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.2.2 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.2.1 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.2.0 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.1.4 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.1.3 lib/locomotive/middlewares/seo_trailing_slash.rb
locomotive_cms-2.1.2 lib/locomotive/middlewares/seo_trailing_slash.rb