Redirect WWW to non WWW

How to Redirect WWW to non WWW in HTACCESS

Redirect WWW to non WWW

Before launching a new website, one of the decisions you will have to make is choosing between www and non-www URL format and redirect www to non www or vice versa.

For example, the www version of your domain could look something like this:

https://www.mydomain.com

On the other hand, the non-www version usually takes this format:

https://mydomain.com or just mydomain.com

Technically speaking – especially from an SEO point of view – these two URLs aren’t the same.

No matter the URL format you go for, it’s important you create rewrite rules in your web server which redirects www requests from visitors to non-www, or, perhaps, redirect nonwww to www. This holds true regardless if your website runs on a dedicated server or an apache server.

In case you are stuck and don’t know which URL format to go for, this article – being one of our ongoing WordPress tutorials – will help you address that problem. What’s more, you will get to learn how to create redirect rules by editing your htaccess files. So stick around!

In this article:

WWW or Non-WWW: Which Should I Go for?

The truth is, whichever you go for doesn’t really matter, it is not that important, like for example Http to https. It all boils down to your personal preference. Oftentimes, most webmasters decide between nonwww to www  or to redirect www to nonwww based on aesthetics.

Non-www URLs are shorter and oftentimes cooler to look at, compared to their www counterpart. Other than that, there’s nothing else.

None has an SEO advantage over the other, and none has a faster load time than the other.

What matters is specifying which format you prefer, and then redirect visitors to your chosen URL format.

How to Force WWW or Non-WWW with htaccess redirect

To edit your htaccess file, you need the means to access it. The easiest way to access and edit the .htaccess file is via the cPanel. Most server hosting plans come with cPanel, but if it happens yours doesn’t have, reach out to your WordPress hosting company.

We will show you how shortly.

Using cPanel

First off, login to your cPanel dashboard. To learn more about cPanel and how to access the login URL, read our detailed article about cPanel.

Upon login, click File Manager

access file manager in cPanel

Next, scroll down to the public_html section and double click on it.

Open public html folder

Upon double-clicking, you will see the .htaccess file.

If it happens you couldn’t find the .htaccess file, chances are that it is hidden. To display the hidden file, click on the Settings button at the top right corner of the screen. A dialog box will pop open as soon as you click the button. In the box, check Show hidden dotfiles and click Save.

show hidden dot files in cPanel

You should now see the .htaccess file.

Right-click on it and Edit. You will be asked to confirm if you want to edit the .htaccess file. Confirm the edit.

To redirect to www from non-www, copy and paste the following lines of code at the bottom in the .htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]

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

 

Ensure you replace yourdomain.com with the actual domain of your website. Once you are done, click the save button.

edit htaccess to redirect www to non www or vice versa.

If you would like to do the reverse, that is force www to redirect to non-www, use the following code instead. It will force redirect from www to non-www version of your website.

 

RewriteEngine On

RewriteRule ^(.*)$ http://www.yourdomain.com [NC]

RewriteCond %{HTTP_HOST} ^yourdomain.com/$1 [L,R=301]

 

Don’t forget to save. And with that, you are done.

Conclusion

To avoid duplicate content problems and hence poor SEO performance, it’s always important to specify whether you would like to use www URL or the non-www version. When you have chosen which fits your business the best, next task is to force redirects to the version you chose.

In this article, we showed you how to do so seamlessly. If you have questions, don’t hesitate to reach out.