How To Do 301 Redirect from www to Non-www Version?

Duplicate content is common in website content. Do you know that duplicate content can happen because of website URLs? Yes, the different formats of URLs whereby some with www while some without, HTTPS and HTTP can cause duplicate content. We will look at how to simply solve this problem using 301 Redirect.

Nowadays, in some cases, Google and most other search engines are smart enough to know if the duplication is caused by URL variation, and Google will index only one version. Having said that, it is a good practice to explicitly tell Google which is our preferred version to eliminate the guessing game and relieve Google from any unnecessary Burden. (Note that Google Spiders have to crawl millions of pages daily!)

The code 301 tells Google that the page moved permanently. Since the URL structure changes are permanent, we want to use 301 instead of 307, which is temporary.

How To Implement 301 Redirect

The easiest method is to edit the .htaccess file for your website. In a nutshell, .htaccess is a configuration file that provides an easy way to make global settings changes to the entire site or directory level.

There are 2 common ways how you can edit the .htaccess file.

(i) Using a plugin like Rankmath (if WordPress powers your site)

This is an easy way to access the .htaccess file within WordPress. If you are on other platforms, check out if they have add-ons or plugins that cater to this. And, if you are not on any platform, you can use method 2 coming next. All you have to do is to add the code in the .htaccess file and save the changes.

htaccess edit in wordpress rankmath

(ii) Access .htacess file in File Manager / or FTP Client

The other way (less preferred) is if you are a total newbie regarding hosting and FTP. But the process is simple if you can follow the steps.

  1. To access the .htaccess file using File Manager, log in to your CPanel (the control panel to manage your web hosting). Depending on your hosting, your hosting provider may use Plesk, DirectAdmin, or any other control panel. Check with your hosting company if you are not sure.
  2. Within the Cpanel, look for File Manager (it is usually under Files section)
File Manager in CPanel HostArmada

3. On the left panel of the screen, scroll down and look for .htacess file. Select the file, right-click and choose Edit (if there is a warning pop-up window asking for confirmation, click Edit to confirm or allow)

edit htaccess

4. Scroll to the bottom of the .htaccess file and copy and paste the following code

Important: Remember to replace yourwebsite.com with your website URL

# Begin from www to non www version

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

# End strip www

Alternatively, if you prefer the “www” version (eg https://www.yourwebsite.com), then you want to use this code instead:

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

Conclusion

The 301 Redirect ensures you have a cleaner, well-kept URL, which makes it clear to search engine bots which version to use. Is it compulsory to do this redirect thing? Not really, having said that it makes your site more user-friendly to Google and other search engines, and highly recommend it.

Similar Posts