API GW Permanent 308 redirects

0

I've deployed my NextJS app in ECS via CDK. The basePath of my application is let's say '/abc'. I was able to access the server via API gateway without any issues.

Recently, I've added a configuration in NextJS - trailingSlash: true, which basically redirects any path without a trailing slash to a path ending with trailing slash. I need this for SEO purposes.

After I've made this change, when I deploy my server using a new docker image created, when I access the server via APIGW, I get a 308 permanent redirect issue and the page does not load. This is the behaviour -

  • When I hit the path '/abc', I initially get redirected to '/abc/' and then get continuously redirected to '/abc/' itself, hence a permanent redirect.
  • When I hit the path '/abc/' directly, I get continuously redirected to '/abc/' itself, hence a permanent redirect.

Now when I try and access the server via the network load balancer, everything works as expected -

  • When I hit the path '/abc', I initially get redirected to '/abc/' and then get a 200 response from the server
  • When I hit the path '/abc/' directly, I get a 200 response from the server

Why is this happening? Is there a configuration that I would need to change? I can provide additional information as needed.(Please consider that I'm an AWS beginner). Thank you

1 Answer
0

First, I would suggest to enable Execution Logs on the API and see the messages. Maybe it will give you a clue.

I tried it an it seems API Gateway strips the trailing / and therefore the backend always receives the URL without the trailing / and redirects again. You will be able to see that in the logs and probably also in your backend logs.

Are you using caching in API Gateway? If you are, it may be that API Gateway ignore the trailing / when it looks for a hit, so all requests to /abc and /abc/ are considered the same and GW returns the cached redirect response. (this is just a guess).

profile pictureAWS
EXPERT
Uri
answered 3 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions