set $root_domain ""; # Derive the apex domain from a www-prefixed primary server_name. This keeps the # snippet simple, but it also means callers should not use it on multi-name # server blocks whose first name is not the canonical www host. if ($server_name ~* ^www\.(?.+)$) { set $root_domain $apex_domain; } # Use 307 so POST and other non-GET methods keep their method during # canonicalization instead of being rewritten to GET as with many 301/302 flows. if ($host = $root_domain) { return 307 $scheme://$server_name$request_uri; }