Search code examples
next.jsdigital-oceanjamstack

Redirects in Next.js app on Digital Ocean not working and must use .html suffix for all pages


Is there a way to deploy a Next.js static site on Digital Ocean and get it to work "normally"?

I got an app deployed but every page has to be accessed via the .html suffix, and redirects specified in next.js.config don't seem to work. This breaks a lot of use-cases, and before I go back to Netlify, I wanted to make sure this isn't just a me problem.

I did follow the guide here to generate a static site.


Solution

  • When you export your Next.js site as static (as opposed to running it with node.js), you are serving static HTML, CSS and JS. That's why the redirects in next.config.js don't work.

    To get those redirects working, there are multiple ways:

    1. Serving the Next.js site with node.js

    Next.js' all features including redirects will work if you run and serve it with node.js. How you can do is described in the "Deploying Next.js as a Custom Server" section of the page you shared.

    2. Configuring your web server to handle the redirects

    Since you didn't share what you use as web server, I can't add any details. But for example, searching "nextjs nginx redirect" might help in case you are using nginx.