diff --git a/nginx.conf b/nginx.conf index 9a16f9c1f1..3cbe3d1692 100644 --- a/nginx.conf +++ b/nginx.conf @@ -9,6 +9,12 @@ map $request_uri $has_no_extension { default 0; } +# Detect trailing slash (except root /) and capture path without it +map $request_uri $trailing_slash_uri { + ~^(.+)/(\?.*)?$ $1$2; + default ""; +} + server { listen 0.0.0.0:8080; server_name docs.apify.com docs.apify.loc; @@ -21,6 +27,11 @@ server { set $backend "https://apify.github.io/apify-docs"; resolver 1.1.1.1 8.8.8.8 valid=30s ipv6=off; + # redirect trailing slashes (except root /) - processed before location matching + if ($trailing_slash_uri) { + return 301 $trailing_slash_uri; + } + location = / { if ($serve_markdown) { rewrite ^ /llms.txt last; @@ -34,12 +45,6 @@ server { proxy_pass $backend$uri; } - # remove trailing slashes from all URLs (except root /) - # exact match locations (e.g., location = /sdk/js/) take priority over this regex - location ~ ^(.+)/$ { - rewrite ^(.+)/$ $1$is_args$args? redirect; - } - location / { set $rewrite_condition "$serve_markdown$has_no_extension"; set $proxy_path $request_uri;