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

andreliem opened this Issue Oct 27, 2017 · 4 comments


None yet
2 participants

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)

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)


This comment has been minimized.

andreliem commented Oct 27, 2017

Thanks, yeah that's my conclusion ;)

@andreliem andreliem closed this Oct 27, 2017


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. - redirects to = OK to = 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 =
  const url = req.url
  const env = process.env.NODE_ENV
  const forceDomain = ''

  if (env === 'production' && host !== '') {
    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.


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.