Question

Redirect non-www to www using App Platform

Posted May 18, 2021 444 views
Node.jsJavaScriptDigitalOcean App PlatformNext.js

I need to redirect the non-www to www domain using the App platform of digital ocean. I had to migrate because of this reason of static Ip’s. I really liked your platform and want to stick with it for now.

Using the Nginx droplet will add up the total cost of the month.

Can the DO team share when it will be published for end-users?

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.

×
Submit an Answer
1 answer

Hello,
Redirection is not possible in static sites. However, in-services, you should be able to do this by using .htaccess in the case of apache. You can configure redirection within Nginx or apache accordingly.

Also, if you require it, App Platform can support root domains. Provided your DNS service allows for CNAME flattening or using an ANAME or ALIAS record, you can point the appropriate record to the actual app URL (ex. my-do-app-a1bcd.ondigitalocean.app.) From there everything should work for your root domain “example.com”, and you could create a CNAME record for the “www” host to point to your root.

  • The website is a React.js+Next.js Application and it’s not a static website. If we deploy two copies using CNAME, it will be very bad in terms of search engine optimization(SEO) as Google will mark as duplicate content. Currently, I am using Vercel to deploy my application and it gives out of the box functionality of redirecting non-www domains to www domains with just a click of a button. Also, I will be able to secure my MongoDB if static IP’s is there, so that I can whitelist only that.

    Currently I am using basic plan for deploying my backend but also wanted to deploy my frontend using App Platform.

    I just wanted to know if this feature is planned and when can I expect this feature to be there in App Platform so that I will migrate and use your platform with ease.

    • you can easily handle permanent redirection in nextjs using the below-mentioned code.

      export const getServerSideProps = async (context) => {
          const { req, res } = context;
          if (req.headers.host.match(/^www/) !== null) {
          res.writeHead(301, {
          location: "https://" + req.headers.host.replace(/^www./, "") || bepakistani.pk + req.url,
          });
          res.end();
          } 
          return { props: {} };
          };