Software Engineering
How to forward root domain to www in GoDaddy with full URL?
1 Answer
1
answers
How to forward root domain to www in GoDaddy with full URL?
1
Answer link
There are several posts on Godaddy support forum, mentioning about using the subdomain forwarding in 'Manage DNS' section to forward example.com to www.example.com. The forwarding only works if you exactly type example.com in address bar.
if (req.headers.host.match(/^www/) !== null ) {
res.redirect('http://' + req.headers.host.replace(/^www\./, '') + req.url);
} else {
next();
}
})
But that feature does not work in following cases:
- If your URL is more than a domain name. e.g. example.com/xyz will not get forwarded to www.example.com/xyz
- If you type https://example.com it will not be forwarded to https://www.example.com
If you want the above cases to work, GoDaddy wants you to host your website on their own Hosting platform, so if you are AWS/Google Cloud/Azure/Digital Ocean customer, then you are out of luck with the domain forwarding option in GoDaddy.
How to fix it:
- Remove the subdomain forwarding from GoDaddy's Manage DNS.
- Add A record with @ entry, pointing to the IP address of your hosting server(from where you serve your website)
- Implement the URL handling/forwarding in your website
You need to handle all the root to www traffic on your own, where you have hosted your website. And there are several ways to host your website, so it would depend on how you have implemented your deployment. Here are a couple of examples
- If you are using Kubernetes with ingress controller, then you can add this annotation to your ingress resource: nginx.ingress.kubernetes.io/from-to-www-redirect: "true", more details here: https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#redirect-fromto-www
- If you have plain nginx server then you can redirect the root domain to www using something like: https://stackoverflow.com/questions/7947030/nginx-no-www-to-www-and-www-to-no-www
- If you are not using any load balancers or reverse proxies then you have to redirect the requests in your web server itself. Node.js express example can be written something like:
if (req.headers.host.match(/^www/) !== null ) {
res.redirect('http://' + req.headers.host.replace(/^www\./, '') + req.url);
} else {
next();
}
})