- %r{^https?://([^/]*)(.*)$}.match(url) do |m|
- "#{hosts_replacement}#{m[2]}" if hosts_replacement && hosts&.include?(m[1])
+ %r{^(https?://([^/]*))(.*)$}.match(url) do |m|
+ scheme_host, host, path = m.captures
+ if hosts&.include?(host)
+ path = yield(path) if block_given?
+ if hosts_replacement
+ "#{hosts_replacement}#{path}"
+ else
+ "#{scheme_host}#{path}"
+ end
+ end || url