0

I want to redirect my ghost aplication everytime it matches www.url to url. I've found this two-years post and tried to update the code. I inserted this snippet before the fist router.get (Admin routes), but it doesn't work. What is the correct way to do this? Ghost version is 0.7.4.

// 301 redirect from www to non-www
router.get('/*', function(req, res) {
    if(req.headers.host.match(/^www/) !== null) redirect301(res, 'http://' + req.headers.host.replace(/^www\./, '') + req.url);
});
Mateus Felipe
  • 1,071
  • 2
  • 19
  • 43
  • How are you hosting Ghost? I have mine hosted on AWS so I'm using an A record to alias www to non-www on Route 53 - works a treat. Perhaps you can use your own DNS to do that? – James Murphy Feb 09 '16 at 22:06
  • Is this on Pro or on your own server? If it is on your own server how are you running it? Are you using Nginx or HAProxy in front of it? – JT Turner Feb 10 '16 at 22:34
  • @JamesMurphy It's running on Heroku, with CloudFlare as DNS. Heroku don't provides A information, because the way it works. I've set an ALIAS to non-www, so both addresses works with no redirecting. I should implement the redirection server-side. – Mateus Felipe Feb 10 '16 at 23:08
  • @JTWebMan I run Ghost on Heroku. I though about using NGinx, but until I configure it I may implement the redirect server-side. – Mateus Felipe Feb 10 '16 at 23:08
  • 2
    Mateus best thing you can do is Nginx and redirect then if you can't use DNS and A records. It's not supported by Ghost afaik. You could change ghost but then that's not maintainable easily with future upgrades – James Murphy Feb 10 '16 at 23:11

0 Answers0