How can I redirect facebook crawler bot to another webpage?

Viewed 760

I've developed small website done by PreactJS which has some parts of main website (PHP). As we all know, Facebook bot cannot crawl javascript pre-render content while sharing. That's why I want to redirect as letting facebook bot to crawl main website (PHP) while sharing a link of website done by PreactJS in NodeJS/ExpressJS as follow:

if user is sharing like, 'https://www.mywebsite.com/category/trips/10' I want nodejs/express to redirect 'https://main.mywebsite.com/category/trips/10'. Can anyone tell me how can I do like that in expressjs level as follow:

app.use('/*', function(req, res){
  if(req.headers['user-agent'] === 'facebookexternalhit/1.1') {

  }
});
2 Answers

Instead of redirecting, I strongly recommend proxying that data. Otherwise, Facebook is going to assume that your URL is a redirect for regular users as well.

Consider a module like express-http-proxy, which makes this simple. Untested, but this should get you started:

app.use((req, res, next) => {
  if (
    !req.headers['user-agent'] ||
    !req.headers['user-agent'].startsWith('facebookexternalhit')
  ) {
    return next();
  }

  return proxy('main.mywebsite.com')(req, res);
});
app.use('/*', function(req, res){
  if(req.headers['user-agent'] === 'facebookexternalhit/1.1') {
   res.redirect("https://main.mywebsite.com/category/trips/10")
  }
});

You can use res.render() if you want to redirect it to a view you have created

Related