Sha256: a36492c4ed0adffdfffa0e79b4ae109a1db90f032a43aaec6d6aa48a5b728cd3
Contents?: true
Size: 1020 Bytes
Versions: 28
Compression:
Stored size: 1020 Bytes
Contents
# frozen_string_literal: true module Koi class UrlRedirect def initialize(app) @app = app end def call(env) status, headers, body = @app.call(env) current_path = env["REQUEST_URI"] if status.to_i == 404 && (redirect = UrlRewrite.active.find_by(from: current_path)) request = ActionDispatch::Request.new(env) # Close the body as we will not use it for a 301 redirect body.close # Return a redirect response [redirect.status_code, { "Location" => new_location(current_path, redirect.to, request) }, ["#{redirect.from} moved. The document has moved to #{redirect.to}"]] else # Not a 404 or no redirect found, just send the response as is [status, headers, body] end end private def new_location(current_path, new_path, request) if %r{\Ahttps{0,1}://}.match?(new_path) new_path else request.original_url.gsub(current_path, new_path) end end end end
Version data entries
28 entries across 28 versions & 1 rubygems