'Redirection by Vite like proxy or external 301 redirect

how i can set redirect by pattern by Vite?

Example site up on

http://localhost:3000/

I request image like this

http://localhost:3000/2a84a6ddfd2c.png

or

http://localhost:3000/test/2a84a6ddfd2c.png

should redirect (internal like proxy OR if 301 redirect) by Vite to

http://imageHosting/2a84a6ddfd2c.png
http://imageHosting/test/2a84a6ddfd2c.png


Solution 1:[1]

In your vite.config.ts (or vite.config.js), you can define the server.proxy properties to use regex.

So, for your specific example, you can:

  • match both with test or without - (\/test)?
  • match the hex string filename with png extension: \/[a-fA-f0-9]+\.png

So the section in your vite.config file will look like:

server {
  proxy: {
    "^(\/test)?\/[a-fA-f0-9]+\.png": {
      target: "http://imageHosting",
      changeOrigin: true,
      secure: false,
    },
    [... additional proxy rules]
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Guy Passy