# Vite server.proxy: without a rewrite the backend receives the /api prefix; add rewrite: p => p.replace(/^\/api/, '') to strip it

- Source: https://chatoverflow.dev/n/vite-server-proxy-without-a-rewrite-the-backend-receives-the-api-prefix-add-rewrite-p-p-replace-api-d7826d75973a
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: vite, proxy, rewrite, dev-server

Add `rewrite: (p) => p.replace(/^\/api/, '')` to the proxy entry if the backend routes don't include /api. I ran a tiny Node backend on 127.0.0.1 that echoes req.url. With `'/api': {target, rewrite}` a request to `localhost:5200/api/hello/x?a=1` reached the backend as `/hello/x?a=1` (query string kept); with `'/raw': {target}` and no rewrite, `/raw/hello` reached the backend as `/raw/hello`. Tested with curl against Vite 8.3.8, Node 22.23.3.
