Question

Digital Ocean App Platform not redirecting my domain

I recently deployed my next.js app to Digital Ocean, it’s working perfectly on my domain.bio, however, it wasn’t working on www.domain.bio. So what I did was, I inserted domain.bio as a domain on App Platform domains under my app, then inserted *.domain.bio, which looks like this now:

image alt text
image caption

The problem is, now domain.bio and www.domain.bio are 2 separate websites and logging in one isn’t affecting the other (normally).

I tried to insert CNAME www on my digital ocean domains section, but it kept throwing 421 misdirected request (cloudflare) when I visited www.domain.bio. Currently, my domain DNS looks like this:

image alt text
image caption

What can I do to redirect www.domain.bio to domain.bio?


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.

Hi there,

What I could suggest is to first decide whether you want to use www.domain.bio or domain.bio as your primary domain and then you’ll want to set up a server-side redirect from the non-preferred to the preferred version. This ensures that users and search engines always end up on the version of the site you’ve chosen.

For Next.js deployed on DigitalOcean’s App Platform, you’d typically handle this in your app code using Next.js’s server-side capabilities or custom server logic.

Here’s an example using Next.js server-side logic:

// Inside your getServerSideProps or getInitialProps

export async function getServerSideProps(context) {
    const { req, res } = context;
    const host = req.headers.host;

    if (host === 'domain.bio') {
        res.writeHead(301, {
            Location: 'https://www.domain.bio' + req.url
        });
        res.end();
    }

    // Your other server-side code here...

    return {
        props: {}
    };
}

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

Featured on Community

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