How to Redirect www to Non-www URL in htaccess

How to Redirect www to Non-www URL in htaccess

How to Redirect www to Non-www URL in htaccess?

Before launching a new website, one decision you will have to choose is between the www and non-www URLs.

What is the difference?

The www vs non-www question is that there is no big difference, no matter which you use. However, you will realize that www domains have some slight technical benefits that improve a website's performance.

From an SEO standpoint, the differences are incredibly small, with www domains having some advantages.

The www URL format of your domain could appear something like this:
www.mydomain.com

Adding non-www version, your domain will like this:
https://mydomain.com

From a technical SEO point of viewing your URL, these two URLs are not the same.

No matter the URL format you go choose, it is important to create rewrite rules in your htaccess file which redirects www requests from visitors to non-www or www.

In case you are having issues and don’t know which URL to format your website, this will help you solve that problem with a solution.

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 non-www to www or to redirect www to non-www 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 redirecting visitors to your chosen URL format.

How to Force WWW to Non-WWW with htaccess file.

To edit your htaccess file, you need to access your web files from your FTP client. The easiest way to access and edit the .htaccess file is to log into your cPanel. I prefer to use an FTP client like Fetch or Cyberduck for the purpose of backing up my htaccess file before adding the necessary RewriteEngine rule. This might be a faster approach other than logging into your cPanel, clicking multiple times to access your file manager under your hosting cPanel.

I will show you how to execute this solution now!

If you would like to force www to redirect to non-www, use the following code below. It will force redirect from www to non-www version from your website URL string.

Copy and paste the rule at the top level of your htaccess file.

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

Or for http:// replace the third line with, RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

If would you like to verify the redirect rule, run an SEO performance audit by clicking this link to review your changes.

Conclusion

With this article, you have learned how to access the .htaccess file, write non-www rule to redirect your domain from www to non-www.