Redirect WWW To Non-WWW URLs And Vice Versa

This is one of the worst confusions I had when I started blogging, whether to keep ‘www’ or not. At first I thought ‘www’ is mandatory and removing it would cause errors or something like that. It’s good to keep a constant URL so that both readers and search engines don’t get confused. I redirected www.hellboundbloggers.com to hellboundbloggers.com because I thought like, the domain is already lengthy, why to keep ‘www’ and make it more lengthy.

There is a term for this – “URL Canonicalization” – which means picking the best URL amongst the other existing ones. Wait, other existing ones? What are they?

Redirect WWW To Non-WWW URLs

Even though your domain name is single, it has various versions like example.com, www.example.com, example.com/index.php, etc. Normally search engines are smart enough to identify the best, but it is our duty to make it easier. That’s where URL Canonicalization or Normalization usually comes.

Some users, they normally type ‘www’ for each and every URL they visit, it’s like they thought it is a part of a domain name and they think they shouldn’t avoid while typing. Technically (though it may not look) www.example.com is a subdomain of example.com.

Either way, it’s up to you to decide which one you really need. But I would prefer removing ‘www’ and redirecting it to non-www URL. If you are using Apache or Nginx web server this process is really simple, you just need to place a code in your .htaccess file and header. We have codes for both below:

Redirect WWW To Non-WWW URLs & Vice Versa [Apache]

If you are using Apache web server, then create a .htaccess file in your root directory and paste the required code.

Redirect WWW to Non-WWW URLs

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

Redirect Non-WWW to WWW URLs

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


Don’t change anything in the code, even a small change might cause vital errors. Don’t forget to replace example.com with your domain name.

Redirect WWW To Non-WWW URLs & Vice Versa [Nginx]

If you are using Nginx web server, then unlike Apache you need to use them at top of the page.

Redirect WWW to Non-WWW URLs

server {
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}

Redirect Non-WWW to WWW URLs

server {
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}

These codes are different from the ones we used for Apache but the action is same. Don’t forget to replace example.com with your domain name.

Be careful when you access your server for editing files, if you are not comfortable you can ask your hosting’s support to do that. If you face any other issue, kindly let us know in the comments.

2 thoughts on “Redirect WWW To Non-WWW URLs And Vice Versa”

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top