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.

Redirect www to Non www 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.

Frequently Asked Questions

What is the difference between www and non-www URLs?

The difference between www and non-www URLs is simply a matter of preference. They are both valid and work the same way, but some people prefer one over the other. For example, some people believe that using www is more traditional, while others prefer the cleaner look of non-www URLs.

Why should I redirect www to non-www URLs?

There are a few reasons why you might want to redirect www to non-www URLs. One reason is that it can help with your website’s SEO by consolidating your domain authority. If you have both www and non-www versions of your site, search engines may see them as separate entities, which can dilute your domain authority. Additionally, using a single URL structure can make it easier for visitors to remember and share your website’s address.

How do I redirect www to non-www URLs using .htaccess?

To redirect www to non-www URLs using .htaccess, you can add the following code to your .htaccess file:

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

Replace “example.com” with your own domain name. This code will redirect all requests for the www version of your site to the non-www version.

Where do I find my .htaccess file?

Your .htaccess file is typically located in the root directory of your website. If you’re using a web hosting service, you can usually access your .htaccess file through your hosting control panel or FTP client.

What do I do if I don’t have a .htaccess file?

If you don’t have a .htaccess file in your website’s root directory, you can create one. Simply create a new file in a text editor, add the necessary code (such as the code to redirect www to non-www URLs), and save the file as “.htaccess”. Then upload the file to your website’s root directory.

Do I need to do anything else after redirecting www to non-www URLs?

After redirecting www to non-www URLs, you should make sure that your website’s internal links (such as those in your navigation menu) also use the non-www URL structure. You should also update any external links that you control (such as links from social media profiles or business directories) to use the non-www URL. Finally, you should check your website’s search engine listings and update any instances of the www URL to the non-www URL.

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 any questions, don’t hesitate to reach out.