Kemal routing redirecting a subtree

This code snippet shows you how to redirect an entire subtree and remove the first part of the subtree.

This might come in useful, for example when you have outside links referencing different languages (e.g. /de /fr /it) and content paths in them, and you want to serve everything from the same endpoints.

  # adding redirects for language paths
  get "/de/*" do |env|
      path_parts = env.request.path.split("/", 3)
      new_path = "/"
      if path_parts.size > 2
        new_path = new_path + path_parts[2]
      env.redirect new_path

Crystal Lang

Bonus tip:

puts env.inspect

puts env.request.inspect