Skip to content

🐛 Fix double stage redirect with V2 custom domain mapping#1440

Open
monkut wants to merge 1 commit intomasterfrom
bug/1409-v2-custom-domain-double-stage
Open

🐛 Fix double stage redirect with V2 custom domain mapping#1440
monkut wants to merge 1 commit intomasterfrom
bug/1409-v2-custom-domain-double-stage

Conversation

@monkut
Copy link
Copy Markdown
Collaborator

@monkut monkut commented Mar 11, 2026

Summary

Fixes #1409

  • When a custom domain maps to an API Gateway v2 stage, API Gateway strips the stage prefix from rawPath before invoking Lambda
  • But the handler always set SCRIPT_NAME to /{stage}, causing Django/Flask to construct redirect URLs with a duplicated stage (e.g., /dev/dev/admin/)
  • Now detects whether rawPath actually starts with /{stage} — if not (custom domain case), SCRIPT_NAME is left empty
  • Direct API Gateway v2 access (where rawPath includes the stage) continues to work as before

Test plan

  • Added test_wsgi_v2_custom_domain_no_double_stage — verifies custom domain event produces correct URL without stage prefix
  • Existing test_wsgi_script_name_on_v2_formatted_event_with_stage_in_path still passes (direct API Gateway access)
  • All 25 handler tests pass
  • Full test suite passes (272 tests)

@coveralls
Copy link
Copy Markdown

coveralls commented Mar 11, 2026

Coverage Status

coverage: 74.656% (+0.005%) from 74.651%
when pulling 8d82993 on bug/1409-v2-custom-domain-double-stage
into 7366118 on master.

When a custom domain maps to an API Gateway v2 stage, API Gateway strips
the stage prefix from rawPath before invoking Lambda. But the handler
always set SCRIPT_NAME to /{stage}, causing Django to construct redirect
URLs with a duplicated stage (e.g., /dev/dev/admin/).

Now detects whether rawPath actually starts with the stage prefix.
If not (custom domain case), SCRIPT_NAME is left empty so redirects
produce correct URLs without the stage prefix.
@monkut monkut force-pushed the bug/1409-v2-custom-domain-double-stage branch from 4478ebc to 8d82993 Compare April 6, 2026 06:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Double stage redirect when using custom domain with API Gateway V2

3 participants