How to Redirect www to non-www in WordPress? [& Vice Versa]

How to redirect www to non-www in WordPress?

Some folks may argue that either www or non-www has some SEO advantages over the other.

But the truth is that aside from one might appear more aesthetically pleasing than the other, there is no major difference between them.

What is important though, you need to configure your website properly and make sure the format you choose is constant throughout all your URLs.

In this post, I will show you how to 301 redirect www to non-www or vice versa using 3 easy methods (Nginx servers included).

If you have HTTPS already enabled on your site, make sure to force HTTPS on your WordPress website to prevent getting a duplicate content penalty from Google.

However, before we start, have you ever wondered why so many domains are prefixed by www?

The Origins of WWW in Domain Names

The World Wide Web (WWW or W3 for short) as we know today was invented by the computer scientist Tim Berners-Lee back in 1989 and released to the public in 1991.

While it is not required, web administrators back then used to host the Web part of their domains on the www subdomain to distinguish it from the other services they provide like FTP and mail. So each had its own subdomain.

Also, in the early days of the internet, people were not accustomed to typing and clicking URLs. So prefixing the domain by www made it clear that it is a website address.

Eventually, the www prefix which started as a tradition faded away and non-www become the default.

Now let’s see how to redirect www to non-www or non-www to www using 3 quick methods…

Redirect Using cPanel

The first method is the easiest, but it requires having cPanel installed on your server.

First, log into your cPanel account.

From your cPanel dashboard, go to Domains >> Redirects.

cPanel Domain Redirects

Now depending on whether you want to redirect www to non-www or non-www to www, the configuration will differ a little bit:

Redirect www to non-www

cPanel redirect www to non-www

  • Choose Permanent (301) to make the redirect has a 301 status code.
  • Select your domain from the dropdown menu.
  • Leave the textbox after the slash (/) empty.
  • Enter your website URL in the Redirects to textbox (no forwarding slash).
  • Make sure you choose Only redirect with www. option.
  • Leave Wild Card Redirect unchecked.

Redirect non-www to www

cPanel redirect non-www to www

  • Choose Permanent (301) to make the redirect has a 301 status code.
  • Select your domain from the dropdown menu.
  • Leave the textbox after the slash (/) empty.
  • Enter your website URL prefixed with www in the Redirects to textbox (no forwarding slash).
  • Make sure you choose Do Not Redirect www. option.
  • Leave Wild Card Redirect unchecked.

Redirect Using .htaccess File

The second method is by editing the .htaccess file.

First, you need to open the .htaccess file using a code editor.

The file will be located in the root folder of your website (public_html for most users) and its name prefixed with a dot.

If you have cPanel, it is easy to open the .htaccess using the built-in code editor (right-click on the file and choose Edit then Edit again.

WordPress htaccess file cPanel

Redirect www to non-www

If you want to 301 redirect www to non-www, add the below code to your .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
</IfModule>

Replace example.com with your domain. Don’t remove the slash between the domain name and .com in the first example.

Redirect non-www to www

If you want to 301 redirect non-www to www, add the below code to your .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>

Replace example.com with your domain. Don’t remove the slash between the domain name and .com in the first example.

Redirect on an Nginx Server

If you happen to have an Nginx server instead of Apache, editing the .htaccess file will have no effect.

You will need to edit the Nginx config (.conf) files to add the redirect.

Here is an easy guide from DigitalOcean on managing Nginx server blocks.

The default location for Nginx config files is /etc/nginx/conf.d/

Let’s assume your server block (similar to virtual hosts in Apache) is inside the file example.conf.

You need to open this file using a code editor and add the code before the start of your server block.

Redirect www to non-www

To redirect www to non-www, your configuration file should look like this (don’t forget to replace example.com with your domain):

# Redirect www to non-www
# This is the code you need to add
server {
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}


# Your server block (already exists)
server {
    server_name example.com;

    ....

}

Redirect non-www to www

To redirect non-www to www, your configuration file should look like this (don’t forget to replace example.com with your domain):

# Redirect www to non-www
# This is the code you need to add
server {
    server_name example.com;
    return 301 $scheme://www.example.com$request_uri;
}


# Your server block (already exists)
server {
    server_name www.example.com;

    ....

}

Conclusion

Having a proper configuration to 301 redirect www to non-www or vice versa is critical to ensure your site users always end up visiting the same hostname.

When it comes to SEO, it is not important whether you choose www or non-www. What is important is sticking to one hostname and make it consistent throughout all your URLs.

Do you have any questions? Please let me know in the comments section below…

Share

About the author

Hey, I’m Khalil Asmi, a full-stack web developer, and a huge WordPress enthusiast! In the past 6 years, I have been creating WordPress websites and building custom solutions for my amazing clients from all around the world.

Leave a Comment

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