Question

How to create a www redirect for my app

So I have an Nuxt app running on DO. Currently only the ‘naked’ domain works(so without www) and when I visit the www. version it doesn’t work.

So what I have tried is adding a CNAME-record - Hostname www.domain.com - and as value the naked domain(e.g. ‘domain.com’). Now when I try to visit the www. version I get the following Cloudfare error:

Error 1001

DNS resolution error

I tried following this article: https://www.digitalocean.com/community/tutorials/how-to-redirect-www-to-non-www-with-nginx-on-ubuntu-14-04

But I can’t find any nginx or apache folders in the /etc/ folder in the console.

So how would I go about doing this?

Thanks in advance!


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
September 6, 2023

Hi there,

Redirecting from www to a non-www domain (or vice-versa) usually involves two main steps:

  1. Configuring DNS to ensure that both the www and non-www domain names resolve to the correct IP address.
  2. Setting up server-side redirects to ensure that visitors to one version of the domain are seamlessly redirected to the preferred version.

Based on your description, it appears you have a DNS resolution issue, possibly in addition to not having a server-side redirect set up.

Here are a few things that you should check:

1. Check DNS Settings:

First, ensure the DNS settings are correct:

  1. Your A record should be set for domain.com pointing to the DigitalOcean droplet’s IP.

  2. Your CNAME record should have the www version pointing to domain.com. The settings usually look like:

    • Type: CNAME
    • Name: www
    • Value: domain.com

Note: If you’re using Cloudflare, make sure both records (A for domain.com and CNAME for www) are proxied (orange cloud icon) or set to DNS-only (grey cloud icon) based on your preference.

2. Server-Side Redirect:

If you cannot find the Nginx or Apache configurations, it’s possible your application is served by a different server, or perhaps it’s running in a Docker container, or the DigitalOcean App Platform.

However, since you mentioned it’s a Nuxt app, you might be using Nuxt’s built-in server. In this case, you can handle redirects within your Nuxt configuration.

Nuxt Redirect: In nuxt.config.js, you can set up a redirect like this:

export default {
  serverMiddleware: [
    {
      path: '/',
      handler(req, res, next) {
        if (req.headers.host.match(/^www/) !== null ) {
          res.writeHead(301, {
            Location: 'https://' + req.headers.host.replace(/^www\./, '') + req.url
          });
          res.end();
        } else {
          next();
        }
      }
    }
  ]
}

This middleware checks if the incoming request uses the www subdomain and, if so, redirects it to the non-www version.

3. Check Propagation:

After making DNS changes, it might take some time for the changes to propagate. Use tools like DNS Checker to ensure your domain’s DNS is correctly updated worldwide.

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel