301 Redirect from non-www to www for WordPress to resolve Redundant Hostnames

Sometimes few websites are set up in such a way that the example.com and www.example.com will be treated as different websites. Although both URLs have same content and represent the same website, tracking few analytics becomes cumbersome when the domain with and without www does not resolve to the same hostname.

301 Redirect to resolve redundant hostnames - TutLinks
301 Redirect to resolve redundant hostnames – TutLinks

Table of Contents

Resolve Redundant Hostnames warning from Google Analytics

Google will warn you to resolve redundant hostnames with the following message, in case you have Google Analytics tracking configured for your website.

Resolve redundant hostnames. An acutal warning from Google Analytics for the site mfrepublic.com
Resolve redundant hostnames. An acutal warning from Google Analytics for the site mfrepublic.com

Property example.com is receiving data from redundant hostnames. Some of the redundant hostnames are:
example.com
www.example.com
Redundant hostnames are counted as separate rows in reports, so hits that are going to the same page on your site from different hostnames will be split into multiple rows. With data split across multiple rows, traffic to specific pages will appear lower than it actually is.

To avoid this problem, consider setting up a 301 redirect from one of your redundant hostnames to the other, or create a search-and-replace filter that strips “www.” from hostnames.

Google Tag Assistant Recordings can help you verify that your redirect is setup correctly, or that your filter is working as intended.

This is how the warning from Google Analytics will look like

To resolve this problem of redundant hostnames, you need to add a 301 redirect to the website. So the question is where to make changes in order for the domains with and without www resolve to the same hostname?

Approach to add 301 redirect in .htaccess

One of the best and most reliable way to fix this, is in the .htaccess configuration file of the website. In this tutorial we will see how to add a 301 redirect from non-www to www for WordPress to resolve the redundant hostnames.

Before you proceed on a live site to add non-www to www redirect

Sometimes your site is live and many visitors might be accessing it. A simple mistake in the .htaccess may cause damage to both the website and its reputation. In case you are not sure about what you are doing, it is suggested to consult a Software or DevOps professional to fix it for you.

Take backup before editing .htaccess

The .htaccess file is generally located in the root of the website’s directory. Before you make any modifications to the .htaccess file, ensure you take the backup of the .htaccess file.

Code to add in .htaccess to redirect non-www to www URL

Add the following lines that actually redirect your website to www if the non-www URL is visited. To edit the file you can open .htaccess in any basic code editor or notepad of your choice. In case you have access only to Secure Shell (SSH) you can use vim or nano editor based on your comfort to edit .htaccess file. You must ensure you have sudo privileges to edit .htaccess file.

# BEGIN 301 redirect from example.com to www.example.com
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI}  [L,R=301]
</IfModule>
# END 301 redirect from example.com to www.example.com

If you do not have SSL (Secure Socket Layer) enabled for your website then replace https with http in line 5 of the code snippet provided above.

Once you have updated the .htaccess file save and close the file. Clear the browser cache. In case you have any plugin installed to cache your website cache, purge the website cache too.

Open any browser and enter the URL of your website without WWW in it. You should see the website actually redirected to www URL. If you are using Chrome browser, the URL address bar of Chrome automatically hides the http protocol and the www sub-domain. To ensure that, you need to place the mouse pointer in the URL address bar and click there until you see the cursor. Now ensure you see the www in the URL.

Now that the domains with and without www are resolving to the same hostname, you can update the Google Analytics warning as the problem is fixed. Google will take sime time to identify your fix and mark the redundant hostname warning to resolved.

Video Tutorial on adding 301 Redirect from non-www to www

In case you want to see how to add a 301 redirect from non-www to www to a real website, you may want to take a look at this video tutorial below that details on How to add 301 Redirect from non www to www for WordPress.

Navule Pavan Kumar Rao

I am a Full Stack Software Engineer with the Product Development experience in Banking, Finance, Corporate Tax and Automobile domains. I use SOLID Programming Principles and Design Patterns and Architect Software Solutions that scale using C#, .NET, Python, PHP and TDD. I am an expert in deployment of the Software Applications to Cloud Platforms such as Azure, GCP and non cloud On-Premise Infrastructures using shell scripts that become a part of CI/CD. I pursued Executive M.Tech in Data Science from IIT, Hyderabad (Indian Institute of Technology, Hyderabad) and hold B.Tech in Electonics and Communications Engineering from Vaagdevi Institute of Technology & Science.

Leave a Reply