Skip to content

Documentation pages return 404 when the URL contains query parameters #720

In review

Description

@gd-scalingo

Description

On developers.scalingo.com, every page returns a 404 Page Not Found when the URL contains query parameters. The URL without parameters works fine.

Example: https://developers.scalingo.com/databases/index is reachable, but https://developers.scalingo.com/databases/index?query=guillaume-test returns a 404 (curl -I → HTTP/2 404).

Reproduction examples
URL Result
https://developers.scalingo.com/databases/index?query=guillaume-test 404 🔴
https://developers.scalingo.com/apps?test 404 🔴
https://developers.scalingo.com/?utm_source=guillaume-test 200 🟢

Expected behavior

Query parameters should be ignored when resolving pages: a URL with query parameters should serve the same page as the URL without them.

Impact

External services (e.g. ChatGPT, ad campaigns) link to our documentation with UTM query parameters, and their users land on an error page instead of the documentation.

Activity

  1. added
    bug
    on Nov 25, 2026
  2. added theissue type on Nov 25, 2026
  3. gd-scalingo commented on Nov 25, 2026

    @gd-scalingo
    Author

    Root cause

    The production stack is Puma → config.ru (rack-rewrite 1.5.1 + rack-jekyll). The rewrite rule in config.ru is:

    rewrite %r{/(.+)}, lambda { |match, rack_env|
      if File.exist?("_site/" + match[1] + ".html")
        return "/" + match[1] + ".html"
      else
        return "/" + match[1]
      end
    }

    The bug comes from how rack-rewrite matches rules: Rack::Rewrite::Rule#matches? runs the pattern against PATH_INFO + ? + QUERY_STRING (build_path_from_env in rack-rewrite-1.5.1/lib/rack/rewrite/rule.rb). So for /databases/index?query=x, the lambda receives match[1] == "databases/index?query=x":

    1. File.exist?("_site/databases/index?query=x.html") → false, so the extensionless branch is taken and the target is /databases/index?query=x.
    2. Rule#apply! splits the target at ? and sets PATH_INFO = "/databases/index" — without the .html suffix the rule exists to add.
    3. rack-jekyll's FileHandler#get_filename only tries _site/<path> and _site/<path>/index.html — never _site/<path>.html. /databases/index resolves to nothing → 404.

    This also explains the non-uniform behavior reported:

    • Without a query string, the File.exist? check succeeds (e.g. _site/databases/index.html) → rewritten to the .html file → 200.
    • /?utm_source=... works by accident: the rule captures ?utm_source=..., apply! splits it off, and PATH_INFO ends up as / → _site/index.html.
    • /apps?test 404s for the same reason (the file is _site/apps.html, but the rule looks for _site/apps?test.html).

    Proposed fix

    In config.ru, build the target from rack_env["PATH_INFO"] (which never contains the query string, per the Rack spec) instead of the regex capture, and re-append the query string so downstream consumers (analytics, client-side JS) keep it:

    use Rack::Rewrite do
      rewrite %r{/(.+)}, lambda { |match, rack_env|
        path = rack_env["PATH_INFO"]
        target = File.exist?("_site#{path}.html") ? "#{path}.html" : path
        qs = rack_env["QUERY_STRING"]
        qs.nil? || qs.empty? ? target : "#{target}?#{qs}"
      }
    end

    Note: if the query string is dropped instead of re-appended, Rule#apply! sets env["QUERY_STRING"] = "", which would silently strip UTM parameters — worth avoiding given the issue mentions ad campaigns.

    An alternative is to change the regex to %r{/([^?]*)} so the query string is never captured, but the branch must still re-append QUERY_STRING for the same reason; using PATH_INFO directly is simpler.

    Verification

    I reproduced both the bug and the fix by running the exact middleware rule against a stub _site tree with the vendored rack-rewrite 1.5.1 and rack-jekyll's FileHandler#get_filename resolution logic. Results:

    URL Current behavior With the fix above
    /databases/index 200 200
    /databases/index?query=guillaume-test 404 200 (query preserved)
    /apps?test 404 200 (query preserved)
    /?utm_source=guillaume-test 200 200
    /nonexistent?x=1 404 404
    Reproduction script (rack-rewrite rule + rack-jekyll FileHandler logic)
    require "rack"
    require "rack/rewrite"
    require "fileutils"
    
    SITE = "/tmp/site"
    FileUtils.mkdir_p("#{SITE}/databases")
    File.write("#{SITE}/databases/index.html", "databases index")
    File.write("#{SITE}/apps.html", "apps")
    File.write("#{SITE}/index.html", "home")
    
    app = ->(env) { [200, {}, []] }
    rewriter = Rack::Rewrite.new(app) do
      rewrite %r{/(.+)}, lambda { |match, rack_env|
        path = rack_env["PATH_INFO"]
        target = File.exist?("_site#{path}.html") ? "#{path}.html" : path
        qs = rack_env["QUERY_STRING"]
        qs.nil? || qs.empty? ? target : "#{target}?#{qs}"
      }
    end
    
    # Rack::Jekyll::FileHandler#get_filename
    class Resolver
      def initialize(root)
        @root = File.expand_path(root)
        @files = Dir[@root + "/**/*"].reject { |f| FileTest.directory?(f) }
      end
      def get_filename(path)
        fullpath = File.join(@root, Rack::Utils.unescape(path))
        normalized =
          if fullpath.end_with?("/") then fullpath + "index.html"
          elsif !@files.include?(fullpath) then fullpath + "/index.html"
          else fullpath
          end
        @files.include?(normalized) ? normalized : nil
      end
    end
    
    resolver = Resolver.new(SITE)
    Dir.chdir("/tmp") do
      ["/databases/index", "/databases/index?query=x", "/apps?test", "/?utm_source=a"].each do |url|
        env = Rack::MockRequest.env_for("http://localhost#{url}")
        rewriter.call(env)
        file = resolver.get_filename(env["PATH_INFO"])
        puts format("%-42s => %s (QS=%p)", url, file ? "200" : "404", env["QUERY_STRING"])
      end
    end

    Once the fix is deployed, the following should all return 200: /databases/index?query=guillaume-test, /apps?test, /?utm_source=....

  4. self-assigned this
    on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions