Need a rewrite rule to pass status code but not redirect

I need that when a url under /api/* is matched it will rewrite but not change the status codes passed from the upstream server.

The issue with what I’ve found is that if I add a 200 status it rewrites, but I miss the upstream status. If I add 301 I keep the upstream status but I encounter cors.

Seems like a trivial task but from some reason I can’t find it, what am I doing wrong?

# example netlify.toml
  command = "npm run build:prod"
  functions = "netlify/functions"
  publish = "dist"

  ## Uncomment to use this redirect for Single Page Applications like create-react-app.
  ## Not needed for static site generators.
    from = "/api/*"
    to = ""
    status = 401
    from = "/api/*"
    to = ""
    force = true
    status = 200
    from = "/*"
    to = "/index.html"
    status = 200

  ## (optional) Settings for Netlify Dev
  #  command = "yarn start" # Command to start your dev server
  #  port = 3000 # Port that the dev server will be listening on
  #  publish = "dist" # Folder with the static content for _redirect file

  ## more info on configuring this file:

If the upstream server sends the headers, the browsers will parse it. Netlify Rewrites do little to no filtering of headers received from upstream servers. So if the upstream server sends a 301, we pass it as it is to the browser and browser takes the necessary action. This cannot be contolled.

I’d advise creating a Function or an Edge Function that will follow all the redirects, fetch that data and return the final output.

1 Like

You are right, the “bad rewrite” I’ve has was actually on the upstream server and it happened since I’ve configured a wrong path for the api.


Hi @adam.genshaft :wave:t6: thanks for letting us know what worked for you. We appreciate the feedback.

1 Like