Question

How can I redirect traffic from an existing subdomain to a new subdomain?

Posted February 19, 2020 114 views
Ubuntu

Our company recently went through a rebrand resulting in a change of domain.

We previously had users accessing our app (Ubuntu droplet with NodeJS) via app.olddomain.com.

I have successfully updated the DNS to have the droplet be served on app.newdomain.com instead (which is working completely fine), but would like anyone who mistakenly goes to the old access point (app.olddomain.com) to be redirected to the new subdomain (app.newdomain.com).

DNS is being managed through DigitalOcean, and both domains are purchased through Google Domains.

I have tried a CNAME, but I am seeking a true redirect than an alias.

One option I have considered is hosting a LAMP droplet on app.olddomain.com and use Htaccess to do a 302 redirect, but that seems silly hosting and paying for a droplet just for that purpose.

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.

×
1 answer

Hello,

If you have a webserver like Apache or Nginx installed on your Node Droplet, you could create a separate Vhost or server block for the old domina name and add the redirect there.

If this is not the case, you could try implementing the logic into your Node application directly. You could check if the host matches your old domain name add the redirect to the new one if this is the case.

You could use something like this:

app.use(function forceLiveDomain(req, res, next) {
  var host = req.get('Host');
  if (host === 'app.olddomain.com') {
    return res.redirect(301, 'https://app.newdomain.com/' + req.originalUrl);
  }
  return next();
});

Personally I would go with the Nginx or the Apache redirect so that I don’t have to change the application logic.

Hope that this helps!
Regards,
bobby

Submit an Answer