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.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF