We maintain an api with Symfony 5 running on PHP 8 hosted on Heroku.
Every route is prefixed with /api
, e.g. /api/myresource
. Symfony router includes a built-in 301 redirect for routes with trailing /. Therefore, a request GET https://example.herokapp.com/api/myresource/
is answered with a redirect
GET https://example.herokapp.com/api/myresource/
HTTP 301
Location: https://example.herokapp.com/api/myresource <-- no trailing slash
Our api is accessed through akamai CDN which also acts as a http cache. On akamai the api is configured as backend for /my-backend-api
e.g.
https://example.com/my-backend-api/myresource
points to https://example.herokapp.com/api/myresource
.
That works fine so far, but our problem comes with the trailing slash redirect feature. When we ignore the X-Forwarded-Host
-Header on the backend api, we get redirects that bypass the CDN:
GET https://example.com/my-backend-api/myresource/
HTTP 301
Location: https://example.herokapp.com/api/myresource
This breaks our caching.
When we respect X-Forwarded-Host
its even worse, because
GET https://example.com/my-backend-api/myresource/
HTTP 301
Location: https://example.com/api/myresource <-- wrong path prefix
Which results in a 404 because the prefix /my-backend-api
that our cdn expects is missing.
How can we handle that situation? Is there a simple way to set a route prefix depending on the hostname? Would it be wiser to disable trailing-slash-redirect completely and respond with 404 on trailing slashes or treating them as alias and respond with 200? What would be the best way to do that?