Question

Problems with redirecting root to subdomain

I have currently a problem with the whole domain thing. What i want to achieve is that my app on app platform is accessible from mydomain.com and www.mydomain.com but the root should always redirect to the www subdomain.

What i did before was i added both domains on namecheap and then the app was accessible from root and www. but since i use localstorage in my app that is a problem. and if i just added www and redirected the root to that it was only possible with http://mydomain.com and not https://mydomain.com.

How could i solve this? Would it be easier if i let DO manage my domain (nameserver) and what records would i have to add then

thanks


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
May 19, 2023
Accepted Answer

Hi there,

Yes, letting DigitalOcean manage your domain might make the process easier because it allows you to manipulate DNS records directly from their dashboard.

However, regarding the redirect itself from the non-www to the www version of your domain, I don’t think that it is currently natively supported by the App Platform. You could handle the redirect on your the application level rather than the App Platform itself.

Depending on the language and framework you’re using for your app, the method to implement the redirection will differ.

For example, in a Node.js Express app, you could do something like this:

app.use((req, res, next) => {
  if (req.hostname === 'mydomain.com') {
    return res.redirect(301, `https://www.${req.hostname}${req.originalUrl}`);
  }
  next();
});

I recently answered a similar question about Django as well.

Feel free to share more details about the framework that you are using and I will be happy to make a suggestion!

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/

Hope that helps!

- 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