How do I redirect www.example.com to example.com
Hi there
If this has been answered before I apologize.
I want to make www.example.com redirect to example.com.
Is that something that can be done in the NS records?
Thank you!
1 Reply
This can be accomplished by doing the following:
- Creating the appropriate DNS records for the primary domain and www subdomain
- Creating a redirection in your specific web server configuration for your website
DNS Records:
Assuming you already have propagated A and Quad-A/AAAA records for example.com, you could either create a new subdomain A/AAAA Record or a CNAME for www. For the sake of simplicity, I might recommend the CNAME over two additional A/AAAA records in case your IP ever changes in the future.
The configuration would something like this in Cloud Manager:
Hostname: www
Alias To: @ (root/primary domain)
TTL: Default or specified time
Keep in mind that DNS propagation is not immediate, so subsequent steps may require a slight pause before continuing. I'm a personal fan of using DNS Checker to view global propagation after making changes to any of my domains.
Web Server Redirect:
Your web server's server block needs to be configured to redirect www to the primary domain (or visa versa depending on preference). The configuration will differ between NGINX and Apache, so I've included StackOverflow discussion about both methods:
- http redirect - Nginx no-www to www and www to no-www - Stack Overflow
- apache redirect from non www to www - Stack Overflow
Assuming that you intend to generate an SSL certificate for HTTPS encryption, you would only really need to create the HTTP block since tools like CertBot/LetsEncrypt generally modify the server block accordingly to enable HTTPS traffic. For more information about how to use this tool, be sure to select your Linux Distribution and web server from the list below:
Once completed, users can securely navigate to your domain using both the primary and www subdomains.