Question

How to redirect www to non www on app platform

Hello, there does not seem to be an answer for this that seems to be valid. I have a site where the www.site.com and site.com both work. I want to redirect all www.site.com request to site.com. DNS settings are fine. I need to perform an http redirect but not sure how I can modify the server settings my app is running on.


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
December 14, 2023

Hi there,

I recently answered a similar question here:

https://www.digitalocean.com/community/questions/app-redirect-from-non-www-to-www

I believe that this should be doable as you are already using DigitalOcean for your DNS management:

  • First add both the www and the non-www domains to your App Platform:
  • This will automatically take care of the DNS records for you so you don’t need to manually add any new recrods.
  • Then, you can to set up an HTTP redirect from the www version to the main domain. To do this, you can modify your Node.js application code to handle the redirection logic. Here is a quick example:
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;

app.use((req, res, next) => {
  if (req.hostname.startsWith('www.')) {
    return res.redirect(301, `https://${req.hostname.slice(4)}${req.url}`);
  }
  next();
});

// Your other routes and middleware go here

app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});

In this example, the middleware checks if the incoming request’s hostname starts with www.. If it does, the request is redirected to the non-www version of your domain with a 301 redirect status code. You can adjust the example as necessary to match your application’s environment and programming language.

Let me know how it goes!

If you would like to see this implemented as a feature, the best thing to do to get your voice heard regarding this would be to head over to our Product Ideas board and post a new idea, including as much information as possible for what you’d like to see implemented.

https://ideas.digitalocean.com/

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