New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help - force www or "non www" version of a site #1970

Closed
andreliem opened this Issue Oct 27, 2017 · 4 comments

Comments

Projects
None yet
2 participants
@andreliem

andreliem commented Oct 27, 2017

Does anyone have any tips or snippets which can force a specific domain? Either forcing the www or non www version? Is there anything like the redirect-ssl that could be used?

Or would you just recommend handling this at the DNS level?

This question is available on Nuxt.js community (#c1754)
@KonstantinVlasov

This comment has been minimized.

KonstantinVlasov commented Oct 27, 2017

I think you should do it at the DNS or server level (i.e. nginx)

@andreliem

This comment has been minimized.

andreliem commented Oct 27, 2017

Thanks, yeah that's my conclusion ;)

@andreliem andreliem closed this Oct 27, 2017

@andreliem

This comment has been minimized.

andreliem commented Oct 31, 2017

So actually I've changed my mind. Doing this at the DNS level is not great for me, the DNS I'm using could only redirect at the root level but not with paths.

e.g. -
domain.com redirects to www.domain.com = OK
domain.com/?query=param to www.domain.com/?query=param = FAIL

So I had to do this at the app level. I'm running on Heroku so it's not like I have easy access to the server config.

In short, I've relied on serverMiddleware. I'll probably post up a small repo for this but the snippet for those interested is something like this.

module.exports = function (req, res, next) {
  const host = req.headers.host
  const url = req.url
  const env = process.env.NODE_ENV
  const forceDomain = 'https://www.DOMAIN.com'

  if (env === 'production' && host !== 'www.DOMAIN.com') {
    res.writeHead(301, { Location: forceDomain + url })
    return res.end()
  }

  return next()
}

Then you just need to register this in serverMiddleware in the nuxt.config.js file.

@lock

This comment has been minimized.

lock bot commented Nov 4, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 4, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.