Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
Redirect with 301 status code in react js single page application

I've developed a web application in react js and I'm using the client-side routing. I have a few deleted product URLs and I want to redirect users to the product listings URL with HTTP Status code 301 when the user opens any of the deleted product URLs. For Example, I've one product URL www.abc.com/prod-detail/123 and this product is deleted so when the user opens this URL, I want him to redirect to www.abc.com/products with HTTPS status code 301.

I tried to achieve this using the below solution.

Code to redirect the user to the products listings from the product details.

productDetails.js

if(productNotFound){
window.location.href ="/products?redirect=301";
}

I created one proxy server using http-proxy-middleware in the application to filter the above requests (If products contain the redirect=301 param) and pass them to the Node server.

setupProxy.js

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function (app) {    
    const filter = function (pathname, req) { 
        if(req.query && req.query.redirect==='301'){
            return true;
        }
        else {
            return false;
        }
    };
   
    const apiProxy = createProxyMiddleware(myfilter, {
        target: "http://localhost:8000/",//Node server
        changeOrigin: false,
        secure: false    
    });    
    app.use('/products',apiProxy);
};

In the Node JS server, I'm filtering the requests having query param as redirect=301 and If I found this type of request I just redirect with the status code 301.

inedx.js (Node)

router.get('/products', function (req, res, next) {
    if(req.query && req.query.redirect==='301'){    
    req.query = null;
    const hostUrl = 'http://' + req.get('host') + req.originalUrl.split('?')[0];
    //res.statusCode=301;
    res.redirect(301, hostUrl);
    }
    else{
        res.redirect(301, 'http://' + req.get('host') + req.originalUrl);
    }
});

The above solution is not working for me. I'm not receiving the request on the Proxy server. I'm not sure this is the correct way or not as I'm new to react js.

Thanks in advance for any assistance or insight.

Please also let me know if there is any other way to achieve this without using server-side routing.

I hope the question makes sense. Any help would be appreciated.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!