Bobcares

How to Redirect WWW to Non-WWW on DigitalOcean?

by | Jan 8, 2025

How to Redirect WWW to Non-WWW on DigitalOcean? Let’s see the detailed steps in this article. Bobcares, as a part of our DigitalOcean Managed Service offers solutions to every query that comes our way.

Overview
  1. How to Redirect WWW to Non-WWW on DigitalOcean
  2. Steps to Redirect WWW to Non-WWW on DigitalOcean
  3. Why Redirect WWW to Non-WWW?
  4. Conclusion

How to Redirect WWW to Non-WWW on DigitalOcean?

Consistency is key when it comes to building a seamless user experience and maintaining robust SEO. One common practice is redirecting users from the www version of the website (e.g., www.example.com) to the non-www version (e.g., example.com). For websites hosted on DigitalOcean, this process can be easily configured on the server using Apache or Nginx.

digitalocean redirect www to non www

In this guide, we’ll explain how to implement a 301 redirect—a permanent redirect that helps consolidate traffic and pass SEO equity from the www URL to the non-www version.

Prerequisites

Before we start, ensure the following:

  • Registered Domain Name: W ehave a domain registered and accessible.
  • DigitalOcean Droplet: Server is set up with either Apache or Nginx installed.
  • SSH Access: We can connect to the server via SSH to make configurations.

Steps to Redirect WWW to Non-WWW on DigitalOcean

Step 1: Configure DNS Records

For DigitalOcean’s DNS Manager

Log in to DigitalOcean: Access the Networking section in your DigitalOcean dashboard.

Add A Records:

Root Domain (example.com):

Hostname: @

Will Direct To: The droplet’s public IP.

WWW Subdomain (www.example.com):

Hostname: www

Will Direct To: The same public IP address.

If using an external DNS provider, follow similar steps to add the necessary A records. We must also allow up to 48 hours for DNS propagation.

Step 2: Configure the Web Server

For Apache:

Enable mod_rewrite: Run the command below to enable URL rewriting:

sudo a2enmod rewrite

Edit .htaccess or Virtual Host Configuration:

Using .htaccess: Navigate to the web root directory (e.g., /var/www/html) and create or edit the .htaccess file:

sudo nano /var/www/html/.htaccess

Add the following lines:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Using Virtual Host Configuration: Modify the virtual host file (e.g., /etc/apache2/sites-available/000-default.conf):

ServerName www.example.com
Redirect 301 / http://example.com/

ServerName example.com
DocumentRoot /var/www/html
# Other configurations…

Restart Apache: Apply the changes:

sudo systemctl restart apache2

For Nginx:

Edit Nginx Configuration: Open the site’s Nginx configuration file (e.g., /etc/nginx/sites-available/default):

sudo nano /etc/nginx/sites-available/default

Add Redirect Block: Add a block for redirecting www to non-www:

server {
listen 80;
server_name www.example.com;
return 301 http://example.com$request_uri;
}

server {
listen 80;
server_name example.com;
root /var/www/html;
# Other configurations…
}

Restart Nginx: Save changes and restart the server:

sudo systemctl restart nginx

Step 3: Test the Redirect

Using curl: Check the redirect with:

curl -I http://www.example.com

Look for a 301 Moved Permanently status and a Location header pointing to http://example.com.

Manually Test: Visit both www.example.com and example.com in a browser to confirm redirection.

Why Redirect WWW to Non-WWW?

Consistency: Avoids confusion by directing users to a single URL format.

SEO Benefits: Prevents duplicate content issues and consolidates link equity.

User Experience: Ensures users land on the intended version of the website.

[Need to know more? Get in touch with us if you have any further inquiries.]

Conclusion

By following these steps, we can effortlessly redirect www traffic to non-www on DigitalOcean, enhancing the website’s performance and SEO ranking.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.