0

I'm using a customized version of the vhost connect/express middleware and inside it I check for the www subdomain. If I find that subdomain, then I redirect to the host + path without the www. So for example, if I go to www.google.com I want to redirect to google.com. I call:

if (subdomain === "www") {
  res.redirect(req.headers.host.split('.').slice(1).join('.') + req.url);
}

However, this redirects me to: www.google.comgoogle.com as it appends the new url to the original one. Why is it doing this?

aynber
  • 22,380
  • 8
  • 50
  • 63
user730569
  • 3,940
  • 9
  • 42
  • 68

1 Answers1

2

Try prepending the protocol:

res.redirect('http://' + req.headers.host.split('.').slice(1).join('.') + req.url);
Linus Thiel
  • 38,647
  • 9
  • 109
  • 104