Redirect a www URL to the equivalent not www

This article is about how to redirect a generic domain like to its equivalent

The solution should work on any generic site (not just WP), and for any generic URL (which makes my life easier).

Here's how you can do it.

Note - If you have a single site with a single domain, you can skip directly to point 2.

1. Register the new domain

First you need to register the domain with your VPS. In other word you need to register a domain with the specific IP at which the VPS responds.

If you are on DigitalOcean, that means to add the new domain to your DNS records and associate it with a specific IP (Droplet). From the Dashboard open DNS and add the new domain, matching it with the desired Droplet (select from Choose Droplet). You might need to do this for the primary domain as well.

If you deal with cPanel, and it is not the primary domain, you need to park the new domain with your VPS. Under Domains, open Parked Domain, and add the new domain to the VPS.

2. Set a wildcard

Next you have to allow for wildcards. That means giving the VPS the capability to handle subdomains like, and similarly as well.

In DigitalOcean that means to create a CNAME record for the new domain. Open DNS, click on the small magnify icon at the right of the domain you need to edit, and add a CNAME record as shown below.


Note the . at the end of the domain. Do not forget that.

In cPanel simply open Subdomains, and add a * to the primary domain. The primary domain can be your unique domain for a single site, or the primary domain/site of your WP Multisite.

3. Rewrite rule for .htaccess

Finally, you need to tell Apache (the software that runs the most of the servers at today) to remove the www in front of any given domain. To do so you need to find and open the main .htaccess file.

This file is to be found in the root of the public folder or directory where all your site is. It can be something like /var/www/html, or public_html. If you do not have it, you need to create it.

Since it has a . in front, make sure that you can see hidden files, otherwise it can be present, just not visible to you.

Note - When I edit the .htaccess file I usually make a backup first, because messing up with it can cause havoc to your site.

Once you have a safe copy of it, copy the following code and past it at the top of your .htaccess file.

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

Note - In many computer language, as well in Apache syntax, a line that starts with a # is treated as a simple comment. You can use comments to add reminders.

4. Takeways

When dealing with redirections or rewrite rules in Apache, or DNS, or anything directly connected with URL and domains, we better be patient. Often it takes some time before we can see any change.

In this case, your site should start removing the www from the domain after a latency of few minutes. So be patient, and do not panic.

Adding the www to domains

In case you'd like to do the opposite, redirecting to, substitute the last line in the code for the .htaccess file with the one listed below.

RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]