Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

301 redirect with Webpack dev server

I'm trying to redirect urls from this format:

http://localhost:8080/setup/xyz/?code=some-code

to this:

http://localhost:8080/app/#/setup/xyz/?code=some-code

I've tried with both proxies and rewrites:

historyApiFallback: {
  rewrites: [
    { from: /^\/$/, to: '/index.html' },
    { from: /^\/app/, to: '/app.html' },
    { from: /^\/setup/, to: '/app/#/setup' },
  ]

},

But it doesn't seem to work. Is there a way to write 301 redirects using the dev server?

like image 252
Costantin Avatar asked Aug 15 '26 12:08

Costantin


1 Answers

@Eugen's answer put me on the right path. Here are the additional details I needed:

The current devserver API for this is onBeforeSetupMiddleware, which exposes the express server API, including redirects. For your case, I think you'd want something like:

  devServer: {
    onBeforeSetupMiddleware: (devServer) => {
      devServer?.app?.get('/setup', (req, res) => {
        res.redirect('/app/#' + req.originalUrl);
      });
    }
  },
like image 140
Brian McBarron Avatar answered Aug 18 '26 03:08

Brian McBarron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!